2012-06-20 11 views
7

Tôi đang thêm một mục mới vào cuối UITableView và sau khi chèn mục, tôi muốn UITableView cuộn xuống dưới cùng để hiển thị mục mới được chèn vào . Các mục mới được lưu vào Core Data và UITableView được cập nhật tự động bằng NSFetchedResultsController.Di chuyển đến UITableViewCell cuối cùng bằng cách sử dụng scrollToRowAtIndexPath: atScrollPosition: hoạt hình bằng cách sử dụng phương thức NSFetchedResultsControllerDelegate

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

    .... 
} 

Điều này dẫn đến lỗi ngoài giới hạn, tôi dường như không làm cho nó hoạt động. Tôi có thể cuộn đến giây để bình luận cuối cùng bằng cách điều chỉnh hàng của đường dẫn chỉ mục, nhưng tôi không thể đến mục cuối cùng.

Về cơ bản, tôi đang thêm nhận xét vào một bảng nhận xét và sau khi nhận xét được thêm, tôi muốn bảng cuộn đến nhận xét gần đây nhất.

Trả lời

16

Bạn cần phải gọi endUpdates để các tableView có thể tính toán các phần và các hàng mới của nó. Một trường hợp đơn giản sẽ trông như thế này:

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

Khi bạn sử dụng NSFetchedResultsController, nó phức tạp hơn một chút, như các cuộc gọi làm beginUpdates, insertRowsAtIndexPaths:withRowAnimation:, và endUpdates thường trong các phương pháp đại biểu khác nhau. Những gì bạn có thể làm sau đó là

  1. thêm một tài sản insertedIndexPath để lưu trữ các chỉ số đường chèn
  2. sau -insertRowsAtIndexPaths:withRowAnimation: cuộc gọi trong -controller:didChangeObject:atIndexPath:, thêm

    self.insertedIndexPath = insertedIndexPath; 
    
  3. sau [self.tableView endUpdates] trong -controllerDidChangeContent:, thêm

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

cảm ơn @tammo! Điều này làm việc hoàn hảo! – djblue2009

+0

Tạo thuộc tính có tên 'newIndexPath' sẽ tạo lỗi xây dựng: 'Thuộc tính tuân theo quy ước đặt tên Cocoa để trả về các đối tượng 'sở hữu' Chỉ cần đặt tên cho thuộc tính khác:] –

+0

: D Điều này được viết trong thời gian khi trình biên dịch không phàn nàn về điều này. Tôi sẽ thay đổi 'newIndexPath' thành' insertIndexPath'. –

0

Xem nếu điều này giúp ...

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

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