gpt4 book ai didi

ios - NSFetchedResultsController - TableView 中的不完整 UI 更新

转载 作者:可可西里 更新时间:2023-11-01 03:39:26 24 4
gpt4 key购买 nike

我正在使用 NSFetchedResultsController 来刷新 TableView 的数据。数据本身是通过在后台运行的 XML 解析器提供的。解析器完成后,它将数据保存到自己的上下文中。 NSFetchedResultsController 立即获取这些更改并开始为每个更新的元素调用 -(void)controller:didChangeObject:atIndexPath:forChangeType:newIndexPath: 委托(delegate)方法。这也很快,并且在日志文件中看起来完全正常。

但是,在 -(void)controllerDidChangeContent: 中,我调用了 UITableView-(void)endUpdates。然后我在屏幕上看到更新动画,但在所有单元格中,除了最后一个只有一半可见的单元格之外,唯一可见的是单元格左侧的图像。所有文本标签都不可见。大约需要 5 到 10 秒,然后所有标签都会弹出。

但是,如果我忽略 NSFetchedResultsController 的所有委托(delegate)调用并简单地在 -(void)controllerDidChangeContent 上调用 [self.tableView reloadData]: 一切正常。内容立即出现。

有人知道我在这里做错了什么吗?探查器显示主线程基本上什么都不做。除了发送到 TableView 的事件外,触摸事件也得到了正确处理。这些不处理。看起来表格 View 正在忙于做一些严肃的工作,但我真的不知道那会是什么,因为动画已经完成。

这是我对 NSFetchedResultsControllerDelegate 的实现:

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
NSLog(@"%s", __PRETTY_FUNCTION__);
[self.tableView beginUpdates];
}

- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {
NSLog(@"%s", __PRETTY_FUNCTION__);
switch(type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationAutomatic];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationAutomatic];
break;
}
}

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath*)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath*)newIndexPath {
NSLog(@"%s", __PRETTY_FUNCTION__);
UITableView* tableView = self.tableView;
switch(type) {
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
break;

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

case NSFetchedResultsChangeUpdate:
[(NewsItemCell*)[tableView cellForRowAtIndexPath:indexPath] updateWithNews:[self.fetchedResultsController objectAtIndexPath:indexPath]];
break;

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

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
NSLog(@"%s", __PRETTY_FUNCTION__);
[self.tableView endUpdates];
}

这是我的单元格布局代码:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.fetchedResultsController.sections.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
id<NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController.sections objectAtIndex:section];
return sectionInfo.numberOfObjects;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
News* model = [self.fetchedResultsController objectAtIndexPath:indexPath];

NewsItemCell* cell = (NewsItemCell*)[tableView dequeueReusableCellWithIdentifier:NewsCellReuseIdentifier];
[cell updateWithNews:model];
cell.accessoryType = (model.content ? UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone);
return cell;
}

以及单元格的基本更新:

- (void)updateWithNews:(News*)news {
NSString* dateString = [[NSDateFormatter outputDateFormatter] stringFromDate:news.date];
self.headlineLabel.text = (news.headline ? news.headline : NSLocalizedString(@"<NewsNoHeadlineReplacement>", nil));
self.metaInfoLabel.text = [NSString stringWithFormat:NSLocalizedString(@"<NewsMetaInfoFormatDate>", nil), (dateString ? dateString : (NSLocalizedString(@"<NewsNoDateReplacement>", nil)))];

self.readIndicatorView.hidden = (news.read != nil && [news.read compare:news.parsingDate] == NSOrderedDescending);
}

占位符字符串也没有显示。标签完全是空的。只有图像可见!

最佳答案

让我们关注这段代码:

 case NSFetchedResultsChangeUpdate:
[(NewsItemCell*)[tableView cellForRowAtIndexPath:indexPath] updateWithNews:[self.fetchedResultsController objectAtIndexPath:indexPath]];
break;

带您了解正在发生的事情:

  1. 您在 tableView 上打开更新事务
  2. 然后您通过向 tableView 发出命令来响应委托(delegate)调用
  3. 但是对于更新,您通过发出与 tableView 无关的命令来以不同的方式执行
  4. 然后在 tableView 上关闭事务

我的意思是:tableView 命令可以将更改捆绑到一个事务中。似乎您对重新渲染单元格的调用将在下一次交易中结束。所以将上面的代码替换为:

case NSFetchedResultsChangeUpdate:
[tableView reloadRowsAtIndexPaths: [NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic ];
break;

关于ios - NSFetchedResultsController - TableView 中的不完整 UI 更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12881967/

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