2013-03-20 14 views
5

Nota: quando tocco la riga, l'app si arresta in modo anomalo.Errore di asserzione durante l'aggiornamento di tableView

Sto cercando di implementare l'aggiunta di una nuova cella sul tocco di un utente. Ho scoperto che c'era un esempio simile nella dimostrazione della vista tabella del WWDC 2011. Ecco il mio codice dalla mia vista tabella.

Ecco l'errore:

2013-03-19 20:04:28.672 Project[51229:c07] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2380.17/UITableView.m:1070 

Ecco il mio codice dalla vista tabella.

@interface MyPFQueryTableViewController : PFQueryTableViewController <PFLogInViewControllerDelegate, PFSignUpViewControllerDelegate> 

@property (nonatomic, strong) NSIndexPath *controlRowIndexPath; 
@property (nonatomic, strong) NSIndexPath *tappedIndexPath; 

@implementation MyPFQueryTableViewController { 

    ListItemObject *listDetail; 
} 

@synthesize controlRowIndexPath; 
@synthesize tappedIndexPath; 


-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     PFObject *object = [self.objects objectAtIndex:indexPath.row]; 
     [object deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { 
      [self loadObjects]; 
     }]; 
    } 

} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object { 
    static NSString *CellIdentifier = @"listCell"; 

    PFTableViewCell *cell = (PFTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"listCell"]; 
    if (cell == nil) { 
     cell = [[PFTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 


    } 

    // Configure the cell 
    cell.textLabel.text = [object objectForKey:self.textKey]; 
    //cell.imageView.file = [object objectForKey:self.imageKey]; 

    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    [super tableView:tableView didSelectRowAtIndexPath:indexPath]; 

    //if user tapped the same row twice let's start getting rid of the control cell 
    if([indexPath isEqual:self.tappedIndexPath]){ 
     [tableView deselectRowAtIndexPath:indexPath animated:NO]; 
    } 

    //update the indexpath if needed... I explain this below 
    indexPath = [self modelIndexPathforIndexPath:indexPath]; 

    //pointer to delete the control cell 
    NSIndexPath *indexPathToDelete = self.controlRowIndexPath; 

    //if in fact I tapped the same row twice lets clear our tapping trackers 
    if([indexPath isEqual:self.tappedIndexPath]){ 
     self.tappedIndexPath = nil; 
     self.controlRowIndexPath = nil; 
    } 
    //otherwise let's update them appropriately 
    else{ 
     self.tappedIndexPath = indexPath; //the row the user just tapped. 
     //Now I set the location of where I need to add the dummy cell 
     self.controlRowIndexPath = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section]; 
    } 

    //all logic is done, lets start updating the table 
    [tableView beginUpdates]; 

    //lets delete the control cell, either the user tapped the same row twice or tapped another row 
    if(indexPathToDelete){ 
     [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPathToDelete] 
           withRowAnimation:UITableViewRowAnimationNone]; 
    } 
    //lets add the new control cell in the right place 
    if(self.controlRowIndexPath){ 
     [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:self.controlRowIndexPath] 
           withRowAnimation:UITableViewRowAnimationNone]; 
    } 

    //and we are done... 
    [tableView endUpdates]; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if([indexPath isEqual:self.controlRowIndexPath]){ 
     return 45; //height for control cell 
    } 
    return 70; //height for every other cell 
} 

- (NSIndexPath *)modelIndexPathforIndexPath:(NSIndexPath *)indexPath 
{ 
    int whereIsTheControlRow = self.controlRowIndexPath.row; 
    if(self.controlRowIndexPath != nil && indexPath.row > whereIsTheControlRow) 
     return [NSIndexPath indexPathForRow:indexPath.row - 1 inSection:0]; 
    return indexPath; 
} 


@end 
+0

Sono curioso di sapere se l'arresto si verifica sempre quando si deseleziona la riga. – dasblinkenlight

+0

Ah, quando tocco una riga, arresto anomalo. – STANGMMX

risposta

8

Il problema è nel metodo didSelectRowAtIndexPath. Hai:

[tableView beginUpdates]; 

//lets delete the control cell, either the user tapped the same row twice or tapped another row 
if(indexPathToDelete){ 
    [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPathToDelete] 
          withRowAnimation:UITableViewRowAnimationNone]; 
} 
//lets add the new control cell in the right place 
if(self.controlRowIndexPath){ 
    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:self.controlRowIndexPath] 
          withRowAnimation:UITableViewRowAnimationNone]; 
} 

//and we are done... 
[tableView endUpdates]; 

Prima di effettuare tutte le chiamate per raccontare la tabella per aggiungere o rimuovere le righe, è necessario aggiornare l'origine dati con l'aggiunta o la rimozione dei dati. La tabella controllerà quante sezioni e file ci sono prima e dopo le tue righe di aggiunta/rimozione. Il numero di sezioni e righe dopo la modifica deve riflettere correttamente la quantità di dati aggiunti/rimossi con il numero di righe aggiunte/rimosse.

E ovviamente è necessario implementare il metodo numberOfRowsInSection.

+1

Ho inserito - (NSInteger) tableView: (UITableView *) tableView numberOfRowsInSection: (NSInteger) section { PFQuery * query = [PFQuery queryWithClassName: @ "ListItem"]; return query.countObjects; } e sto ancora incontrando lo stesso problema. Cosa intendi per aggiornare la mia fonte di dati? Non è quello che [tableView beginUpdates]; lo fa? – STANGMMX

4

Che aspetto ha il tuo - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section?

Questo errore si verifica quando si tenta di aggiungere o eliminare una riga da UITableView, ma il numero di righe che si afferma di essere nella sezione dopo l'aggiornamento in tale metodo non è coerente con i nuovi dati che devono essere caricati .

Ex, se il tuo numeroOfRowsInSection restituisce sempre 4 e aggiungi una riga in quella sezione, tableView vorrà che sia 5, ma non sarà così si bloccherà. Devi tenere traccia di quante righe ci sono in ogni sezione e restituire quel numero.

+0

Ah, in realtà non l'ho implementato. Sto usando l'SDK di Parse quindi, la vista tabella viene compilata con tutto ciò che spingo in analisi. Quindi ... sembra niente. – STANGMMX

+0

In tal caso, non so come funziona l'SDK, ma assicurati che ogni volta che inserisci una cella nella tabella, stai aggiungendo qualcosa in modo tale che la tabella abbia il numero corretto di celle e lo stesso per l'eliminazione. – Jsdodgers

+0

sì, ho fatto un po 'di google e questo sembra essere il filo conduttore, mal controllato. grazie per il suggerimento – STANGMMX

Problemi correlati