2010-08-14 30 views
6

Ok Các bạn, đây là tình huống tôi có.Chỉ tải lại một UITableViewCell trên UITableview

Tôi đang sử dụng UITableViewController với Dữ liệu chính. Bảng này có khoảng 200 ô, về cơ bản có chức năng như một danh sách kiểm tra. Khi bạn nhấn vào một ô, ô đó có một hộp kiểm được chuyển thành UITableViewCell.imageView (UIImageView được gán ở bên trái nhãn). Bất cứ lúc nào tôi sử dụng một trong hai cách dưới đây để cập nhật bảng, mất khoảng 1 - 2 giây để cập nhật ... và từ những gì tôi có thể nói, có vẻ như nó đang tải lại mọi ô. Làm thế nào tôi có thể buộc chỉ các tế bào đã được khai thác để được cập nhật?

[self.tableView reloadData]; hoặc

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller { 
    [self.tableView beginUpdates]; 
} 

- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo 
      atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type { 

    switch(type) { 
     case NSFetchedResultsChangeInsert: 
      [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] 
          withRowAnimation:NO]; 
      break; 

     case NSFetchedResultsChangeDelete: 
      [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] 
          withRowAnimation:NO]; 
      break; 
    } 
} 

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject 
     atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type 
     newIndexPath:(NSIndexPath *)newIndexPath { 

    UITableView *tableView = self.tableView; 

    switch(type) { 

     case NSFetchedResultsChangeInsert: 
      [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] 
          withRowAnimation:NO]; 
      break; 

     case NSFetchedResultsChangeDelete: 
      [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
          withRowAnimation:NO]; 
      break; 

     case NSFetchedResultsChangeUpdate: 
      [self configureCell:[tableView cellForRowAtIndexPath:indexPath] 
        atIndexPath:indexPath]; 
      break; 

     case NSFetchedResultsChangeMove: 
      [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
          withRowAnimation:NO]; 
      [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] 
          withRowAnimation:NO]; 
      break; 
    } 
} 

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller { 
    [self.tableView endUpdates]; 
} 
+0

Ho Bạn có cập nhật dữ liệu trong mô hình không? Có vẻ như tất cả các ô đều được cập nhật. – geon

+0

tham số thứ hai của '-insertSections: withRowAnimation:', '-deleteSections: withRowAnimation:', '-insertRowsAtIndexPaths: withRowAnimation:', và '-deleteRowsAtIndexPaths: withRowAnimation:' không phải là 'BOOL'; họ là một loại 'UITableViewRowAnimation' – user102008

Trả lời

37

Bạn cần điều này:

- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation 

http://developer.apple.com/iphone/library/documentation/uikit/reference/UITableView_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006943-CH3-SW40

+0

Tôi thực sự tìm thấy điều này một vài phút sau khi đăng câu hỏi và nó hoạt động hoàn hảo. Cảm ơn! – Travis

+0

cảm ơn bạn rất nhiều! –

0

Nếu bạn chỉ cần cập nhật textlabel của tế bào giáo khoa mã này là đủ

UITableViewCell *cell = [_myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]]; 

    cell.textLabel.text = [NSString stringWithFormat:@"%i", (int)_targetMaxDistanceSlider.value]; 
Các vấn đề liên quan