2011-09-08 11 views
9

Si llamo reloadRowsAtIndexPaths para la primera celda de una sección, con la sección anterior vacía y la anterior no vacía, obtengo un extraño error de animación (incluso si especifico "UITableViewRowAnimationNone") donde la celda recargada se desliza hacia abajo desde la sección anterior ..UITableView reloadRowsAtIndexPaths error gráfico

me trataron de simplificar el ejemplo tanto como sea posible:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 3; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
if (section == 0) 
    return 1; 
else if (section == 1) 
    return 0; 
else if (section == 2) 
    return 3; 
return 0; 
} 

- (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] autorelease]; 
} 

// Configure the cell... 
cell.textLabel.text = @"Text"; 

return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
NSArray *editedCell = [[NSArray alloc] initWithObjects:indexPath, nil]; 
//[self.tableView beginUpdates]; 
[self.tableView reloadRowsAtIndexPaths:editedCell withRowAnimation:UITableViewRowAnimationNone]; 
//[self.tableView endUpdates]; 
} 

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
return @"Section"; 
} 

en realidad se puede comentar el último método, pero da una mejor comprensión del problema.

Respuesta

12

Puede establecer los valores que desee directamente en la celda, sin dejar que la tabla se vuelva a cargar (y así evitar cualquier animación no deseada). También para hacer el código más claro y evitar la duplicación de código deja movimiento configuración celular a un método separado (por lo que vamos a ser capaces de llamarlo desde diferentes ubicaciones):

- (void) setupCell:(UITableViewCell*)cell forIndexPath:(NSIndexPath*)indexPath { 
    cell.textLabel.text = @"Text"; // Or any value depending on index path 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    [self setupCell:cell forIndexPath:indexPath]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    // create cell 

    // Configure the cell... 
    [self setupCell:cell forIndexPath:indexPath]; 

    return cell; 
} 
+0

Esa es una gran solución, de todos modos, es el comportamiento extraño de reloadRowsAtIndexPaths un error, o solo lo estaba usando de la manera incorrecta? – Fr4ncis

+0

@ Fr4ncis, no estoy seguro. Para volver a cargar la vista de tabla de celdas puede necesitar agregar/eliminarlos de la jerarquía de vista, reconstruir algunas de sus subvistas u otra cosa - eso depende de cómo se implementan todas esas transformadas internamente – Vladimir

+0

gracias, su solución está limpia y bien estructurada. – jalopezsuarez

Cuestiones relacionadas