gpt4 book ai didi

iphone - NSFetchedResultsControllerDelegate 'ChangeUpdate' 行为是否已损坏?

转载 作者:行者123 更新时间:2023-12-03 18:30:53 27 4
gpt4 key购买 nike

NSFetchedResultsControllerDelegate 的文档提供了以下示例代码

- (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:UITableViewRowAnimationFade];
break;

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

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

case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView reloadSections:[NSIndexSet indexSetWithIndex:newIndexPath.section] withRowAnimation:UITableViewRowAnimationFade];
break;

}

}

当我创建一个新的 NSManagedObject 时,NSFetchedResultsChangeInsert 会触发(太棒了!)。当我更改属性值(用于单元格标题)时,NSFetchedResultsChangeUpdate 会触发。不幸的是,除非我重新加载表、部分或行,否则新标题不会自动显示。事实上,如果新名称导致结果集以不同方式排序,则 NSFetchedResultsChangeMove 会触发,并且一切正常,因为提供的代码会重新加载整个部分。

UITableView 有一个方法reloadRowsAtIndexPaths:withRowAnimation,所以我尝试在 NSFetchedResultsChangeUpdate 代码块下使用它。它确实有效......但是这个特定方法的文档读起来好像我不需要它(注意最后一行):

Reloading a row causes the table view to ask its data source for a new cell for that row. The table animates that new cell in as it animates the old row out. Call this method if you want to alert the user that the value of a cell is changing. If, however, notifying the user is not important—that is, you just want to change the value that a cell is displaying—you can get the cell for a particular row and set its new value.

是的,如果我记录正在发生的事情,当

[self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath]; 

NSFetchedResultsChangeUpdate上调用,它能够检索最新的“名称”值并将其设置在单元格的textLabel中。除非我重新加载,否则该名称不会在单元格中呈现。即使我只是单击单元格,名称也会显示出来。请注意,要重新创建此行为,您必须创建一个新的托管对象,然后为其指定一个名称,使其在 NSFetchedResultsController 中排序为 FIRST。这样, NSFetchedResultsChangeMove 就不会触发(它确实有效,因为它重新加载了该部分)。

我错过了什么还是这是预期的行为? reloadRowsAtIndexPaths 的“讨论”让我相信我应该能够简单地设置单元格的 textLabel,而无需重新加载行、部分或表格。

最佳答案

您应该调用 [cell setNeedsLayout] 或/和 [cell setNeedsDisplay] 来刷新单元格,具体取决于单元格的实现。

如果你像我们通常那样组合 subview 的单元格,你依赖于-layoutSubviews,所以你应该调用[cell setNeedsLayout]

如果直接使用-drawRect:绘制单元格,则应该调用[cell setNeedsDisplay]

如果您同时使用合成和绘图,则应该同时调用两者。

关于iphone - NSFetchedResultsControllerDelegate 'ChangeUpdate' 行为是否已损坏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1326408/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com