2012-06-20 17 views
7

Estoy agregando un nuevo elemento en la parte inferior de una UITableView y después de insertar el elemento, quiero que UITableView se desplace hasta el final para mostrar el elemento recién insertado . Los nuevos elementos se guardan en Core Data y UITableView se actualiza automáticamente utilizando NSFetchedResultsController.Desplácese hasta el último UITableViewCell utilizando scrollToRowAtIndexPath: atScrollPosition: animado utilizando NSFetchedResultsControllerDelegate métodos

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

    .... 
} 

Esto lleva a un error de fuera de límites, no puedo hacer que funcione. Puedo desplazarme hasta el penúltimo comentario ajustando la fila de la ruta de índice, pero no puedo acceder al último elemento.

Básicamente, estoy agregando un comentario a una tabla de comentarios y después de agregar un comentario, quiero que la tabla se desplace al comentario más reciente.

Respuesta

16

es necesario llamar a endUpdates para que el tableView puede calcular sus nuevas secciones y filas. Un caso simple podría tener este aspecto:

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

A medida que usa NSFetchedResultsController, que es un poco más complicado, ya que las llamadas hacen beginUpdates, insertRowsAtIndexPaths:withRowAnimation: y endUpdates son típicamente en diferentes métodos de delegado. Lo que podría hacer a continuación es

  1. añadir un alojamiento insertedIndexPath para almacenar la ruta del índice insertado
  2. después de la -insertRowsAtIndexPaths:withRowAnimation: llamada en -controller:didChangeObject:atIndexPath:, añadir

    self.insertedIndexPath = insertedIndexPath; 
    
  3. después [self.tableView endUpdates] en -controllerDidChangeContent:, añadir

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

gracias @tammo! Esto funcionó a la perfección! – djblue2009

+0

Crear una propiedad llamada 'newIndexPath' generará el error de compilación: 'La propiedad sigue la convención de nomenclatura de Cocoa para devolver 'propiedad' objetos ' Simplemente nombre la propiedad algo diferente:] –

+0

: D Esto fue escrito en un momento en que el compilador no me quejaba de esto Cambiaré 'newIndexPath' a' insertedIndexPath'. –

0

ver si esto ayuda ...

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

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