【问题标题】:Reloading only one UITableViewCell on a UITableview在 UITableview 上仅重新加载一个 UITableViewCell
【发布时间】:2011-03-29 22:45:34
【问题描述】:

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

我正在使用带有 Core Data 的 UITableViewController。该表有大约 200 个单元格,基本上用作清单。当您点击一个单元格时,该单元格有一个复选框,该复选框被设置为 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];
}

【问题讨论】:

  • 如何更新模型中的数据?听起来所有单元格都已更新。
  • -insertSections:withRowAnimation:-deleteSections:withRowAnimation:-insertRowsAtIndexPaths:withRowAnimation:-deleteRowsAtIndexPaths:withRowAnimation:的第二个参数不是BOOL;他们是UITableViewRowAnimation 类型

标签: objective-c uitableview ios


【解决方案1】:

如果您只是更新单元格的 textlabel 文本 - 这段代码就足够了

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

    cell.textLabel.text = [NSString stringWithFormat:@"%i", (int)_targetMaxDistanceSlider.value];

【讨论】:

    【解决方案2】:

    你需要这个:

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

    【讨论】:

    • 我实际上是在发布问题几分钟后发现的,而且效果很好。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多