objective-c - 在 UITableview 上仅重新加载一个 UITableViewCell

标签 objective-c uitableview ios

好的,伙计们,这是我的情况。

我正在使用带有核心数据的 UITableViewController。该表有大约 200 个单元格,基本上用作 list 。当您点击一个单元格时,该单元格有一个复选框,该复选框被切换设置为 UITableViewCell.imageView(分配给标签左侧的 UIImageView)。

每当我使用以下两种方式之一更新表格时,更新大约需要 1 - 2 秒……据我所知,它似乎正在重新加载每个单元格。我怎样才能强制只更新刚刚点击的单元格?

[self.tableView reloadData];

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

最佳答案

你需要这个:

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

关于objective-c - 在 UITableview 上仅重新加载一个 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3485162/

相关文章:

ios - 扩展 UITableViewCell UIElements 约束问题?

ios - Xamarin 在 iOS 中使用自定义 UITableViewCell 时抛出 NullReferenceException

ios - 如何在 iOS6 iPad 中使单元格变宽

android - 如何使用 xCode 继续开发 React Native 项目?

ios - 文档文件夹内容 - 文件无法打开,因为没有这样的文件

objective-c - 在 NSString 搜索栏中不断搜索子字符串

ios - 带有文本的 xcode 不可见按钮

ios - 在 tableViewCell 上列出 firebase 子节点时删除值

objective-c - 使用 Cocoa/objective-C 从整数(年 ... 秒)构建 NSDate

ios - Objective-C - 同时关闭表单和页面表