gpt4 book ai didi

ios - 每次 NSFetchedResultsController 中对应于该单元格的对象发生更改时,我如何使我的单元格看起来像更新?

转载 作者:行者123 更新时间:2023-11-28 20:17:19 25 4
gpt4 key购买 nike

我在我的应用程序中使用核心数据,并通过 NSFetchedResultsController 在 TableView 中使用它。我可以对单元格/数据执行各种操作,例如向左或向右滑动以分别删除或将单元格标记为已读(这也会删除数据)。

在我说的情况下,将单元格标记为已读(这还将相应的 Core Data 对象的 isRead 属性设置为 YES),我希望单元格在视觉上更新来表明这一点。视觉更新使单元格变亮以使其看起来已读(就像已读与未读电子邮件的样子)。

我怎样才能做到这一点? (当对应的对象发生属性变化时更新单元格,即。)

我在 NSFetchedResultsController 的委托(delegate)方法中试过:

- (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:(ArticleCell *)[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
break;

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

NSFetchedResultsChangeUpdate 是被选中的情况,所以 configureCell: 然后被调用,看起来像这样:

- (void)configureCell:(ArticleCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
Article *article = [self.fetchedResultsController objectAtIndexPath:indexPath];

cell.article = article;
}

它调用我的 UITableViewCell 子类,ArticleCell 的自定义 article 属性 setter 。在那种方法中,我基本上从:

- (void)setArticle:(Article *)article {
if (_article != article) {

将要设置的新文章与单元格的现有文章进行比较(因此只有在发生更改时才会更新)。但是,这个 if 语句中的代码永远不会被调用!它直接跳过去并存在该方法。在那个 if 语句中,我有所有自定义代码来根据该文章的属性(isReadkind 等)设置单元格的外观

我该如何处理?我只希望每当与该单元格对应的对象有更新时,该单元格就会反射(reflect)更新。无论是读取状态的改变,还是其他什么。

最佳答案

在 if 语句中,您正在比较对同一对象的两个引用,因此当从 NSFetchedResultsChangeUpdate 类型的更改调用 setter 时,它始终为真。

您并不真的需要 if,只需相信 controller:didChangeObject:atIndexPath:forChangeType:newIndexPath: 方法中的代码,如果方法请求它。

关于ios - 每次 NSFetchedResultsController 中对应于该单元格的对象发生更改时,我如何使我的单元格看起来像更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17537424/

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