2011-09-08 12 views
9

Se chiamo reloadRowsAtIndexPaths per la prima cella di una sezione, con la sezione precedente vuota e quella precedente non vuota, ottengo uno strano problema di animazione (anche se si specifica "UITableViewRowAnimationNone") dove la cella ricaricata scivola giù dalla sezione sopra ..UITableView reloadRowsAtIndexPaths glitch grafico

ho cercato di semplificare l'esempio, per quanto possibile:

- (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"; 
} 

in realtà si può commentare l'ultimo metodo, ma dà una migliore comprensione del problema.

risposta

12

È possibile impostare direttamente i valori desiderati sulla cella, senza consentire alla tabella di ricaricarsi (evitando così animazioni indesiderate). Anche per rendere il codice più chiara ed evitare la duplicazione del codice permette di spostare l'installazione delle cellule ad un metodo separato (così saremo in grado di chiamare da luoghi diversi):

- (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

Questa è una grande soluzione, in ogni caso, è lo strano comportamento da reloadRowsAtIndexPaths un bug, o lo stavo usando nel modo sbagliato? – Fr4ncis

+0

@ Fr4ncis, non ne sono sicuro. Per ricaricare la tabella delle tabelle potrebbe essere necessario aggiungere/rimuovere dalla gerarchia delle viste, ricostruire alcune sue sotto-viste o qualcos'altro - che dipende da come tutte queste trasformazioni sono implementate internamente – Vladimir

+0

grazie, la tua soluzione è pulita e ben strutturata. – jalopezsuarez

Problemi correlati