2012-03-26 21 views
5

Obtuve una aplicación iOS con vista de tabla. Cuando selecciono filas varias veces, la información en el duplicado seleccionado.Texto duplicado en UITableView

Se puede ver en una imagen: enter image description here

agrego la celda de la siguiente manera:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    // Configure the cell... 
    UILabel *label = [[UILabel alloc] init]; 
    Place *item = [source objectAtIndex:indexPath.row]; 
    [label setFont:[UIFont fontWithName:@"PermianSansTypeface" size:15.0]]; 
    label.text =item.name; 
    label.frame = CGRectMake(5, 5, 310, 35); 
    [cell addSubview:label]; 
    return cell; 
} 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Navigation logic may go here. Create and push another view controller. 
    OnePlaceViewController *placeView = [[OnePlaceViewController alloc] initWithNibName:@"OnePlaceViewController" bundle:nil]; 
    placeView.nom = indexPath.row; 
    placeView.source = source; 
    placeView.route = route; 
    placeView.titleView = titleView; 
    /* 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    cell.selected = NO; 
    */ 
    [self.navigationController pushViewController:placeView animated:YES]; 
} 

¿Por qué la información duplicada? Thnx.

+3

Dejé mi bola de cristal en casa, por lo que tendrá que publicar un código. –

+0

Agrego un código. míralo por favor –

+1

@EvanMulawski jaja ... pero al principio, parece ser un problema con la reutilización de células. Se necesitarán tanto 'cellForRowAtIndex' como' didSelectRowAtIndex' para una mejor respuesta. – tipycalFlow

Respuesta

7

Creo que el problema es que se va a añadir UILabel como subvista cada vez. Dado que las celdas se reutilizan, cada vez que se recarga la tabla, se agregan varias UILabel s. Por lo tanto, mi sugerencia sería revisar todas las subvistas de la celda y eliminarlas antes de agregar una nueva etiqueta.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
} 
// Configure the cell... 
UILabel *label = [[UILabel alloc] init]; 
Place *item = [source objectAtIndex:indexPath.row]; 
[label setFont:[UIFont fontWithName:@"PermianSansTypeface" size:15.0]]; 
label.text =item.name; 
label.frame = CGRectMake(5, 5, 310, 35); 
for(UIView *v in [cell subviews]) 
{ 
    if([v isKindOfClass:[UILabel class]]) 
     [v removeFromSuperview]; 
} 
[cell addSubview:label]; 
return cell; 
} 
+0

realmente, debes aceptar mi respuesta. Eso es un enfoque mucho más limpio. No necesita agregar, quitar y volver a crear las etiquetas todo el tiempo. Tampoco tienes que pasar por todas las subvistas. Mi solución es menos memoria intensa y más rápida. Pero elígete a ti mismo – calimarkus

+1

@ jaydee3 No hay duda de que su enfoque es mucho más limpio y eficiente que el mío, pero creo que él pidió la parte del por qué, no cómo. – tipycalFlow

+0

sin ofender, pero SO es una base de conocimiento y siempre debe dar la mejor respuesta posible. la respuesta no es solo para él. – calimarkus

8

está añadiendo una etiqueta cada .. cambiarlo así:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    // Configure the cell... 
    UILabel *label = [[UILabel alloc] init]; 
    Place *item = [source objectAtIndex:indexPath.row]; 
    [label setFont:[UIFont fontWithName:@"PermianSansTypeface" size:15.0]]; 
    label.frame = CGRectMake(5, 5, 310, 35); 
    label.tag = 666; 
    [cell addSubview:label]; 
    // add this unless you're using ARC 
    // [label release]; 
    // [cell autorelease]; 
    } 

    UILabel* cellLabel = (UILabel*)[cell viewWithTag: 666]; 
    cellLabel.text = item.name; 

    return cell; 
}