Domanda

Se chiamo ReloadRowRowSatIndexPaths per la prima cella di una sezione, con la sezione precedente vuota e quella sopra non vuota, ottengo uno strano problema di animazione (anche se specifico "UIbleViewRowanimationNone") in cui la cella ricaricata scorre verso il basso dalla sezione sopra. .

Ho provato a semplificare l'esempio il più 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à puoi commentare l'ultimo metodo, ma fornisce una migliore comprensione del problema.

È stato utile?

Soluzione

È possibile impostare i valori che desideri direttamente alla cella, non consentire alla tabella di ricaricare se stessa (e quindi evitare animazioni indesiderate). Inoltre, per rendere il codice più chiaro ed evitare la duplicazione del codice consente di spostare la configurazione delle celle su un metodo separato (quindi saremo in grado di chiamarlo da diverse posizioni):

- (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;
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top