2012-06-20 4 views
7

Sto aggiungendo un nuovo elemento nella parte inferiore di UITableView e dopo aver inserito l'elemento, voglio che UITableView scorra fino in fondo per visualizzare l'elemento appena inserito . I nuovi elementi vengono salvati in Core Data e UITableView viene automaticamente aggiornato utilizzando NSFetchedResultsController.Scorrere fino all'ultimo UITableViewCell utilizzando scrollToRowAtIndexPath: atScrollPosition: animato utilizzando NSFetchedResultsControllerDelegate metodi

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject 
    atIndexPath:(NSIndexPath *)indexPath 
forChangeType:(NSFetchedResultsChangeType)type 
    newIndexPath:(NSIndexPath *)newIndexPath 
{ 
    switch (type) { 
    case NSFetchedResultsChangeInsert: 
     NSLog(@"*** controllerDidChangeObject - NSFetchedResultsChangeInsert"); 
     [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; 

    //THIS IS THE CODE THAT DOESN'T WORK 
    [self.tableView scrollToRowAtIndexPath:newIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES]; 

     break; 

    .... 
} 

Questo porta a un errore di fuori limite, non riesco a farlo funzionare. Posso scorrere fino al penultimo commento regolando la riga del percorso dell'indice, ma non riesco ad arrivare all'ultimo elemento.

Fondamentalmente, sto aggiungendo un commento a una tabella di commenti e dopo aver aggiunto un commento, voglio che la tabella scorra fino al commento più recente.

risposta

16

è necessario chiamare endUpdates in modo che il tableView può calcolare le sue nuove sezioni e righe. Un caso semplice sarebbe simile a questa:

[self.tableView beginUpdates]; 
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:insertedIndexPath] withRowAnimation:UITableViewRowAnimationFade]; 
[self.tableView endUpdates]; 
[self.tableView scrollToRowAtIndexPath:insertedIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES]; 

Come si usa NSFetchedResultsController, è un po 'più complicato, in quanto le chiamate fanno beginUpdates, insertRowsAtIndexPaths:withRowAnimation: e endUpdates sono in genere in diversi metodi delegato. Che cosa si potrebbe fare allora è

  1. aggiungere un alloggio insertedIndexPath per memorizzare il percorso dell'indice inserita
  2. dopo la -insertRowsAtIndexPaths:withRowAnimation: chiamata in -controller:didChangeObject:atIndexPath:, aggiungere

    self.insertedIndexPath = insertedIndexPath; 
    
  3. dopo [self.tableView endUpdates] in -controllerDidChangeContent:, aggiungere

    if (self.insertedIndexPath) { 
        [self.tableView scrollToRowAtIndexPath:self.insertedIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES]; 
        self.insertedIndexPath = nil; 
    } 
    
+0

grazie @tammo! Questo ha funzionato perfettamente! – djblue2009

+0

La creazione di una proprietà denominata "newIndexPath" genererà l'errore di generazione: 'Proprietà segue la convenzione di denominazione del cacao per la restituzione di oggetti" di proprietà " Basta denominare la proprietà qualcosa di diverso:] –

+0

: D Questo è stato scritto in un momento in cui il compilatore non mi sono lamentato di questo. Cambierò 'newIndexPath' in' insertedIndexPath'. –

0

Vedere se questo aiuta ...

[self.tableView beginUpdates]; 
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; 
[self.tableView endUpdates]; 

[self.tableView scrollToRowAtIndexPath:newIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];