动态更新UITableView节标题

Dav*_*idD 0 iphone core-data objective-c uitableview ios

我想异步下载和更新自定义节标题,但我找不到办法.我目前UIView使用以下方法制作自定义:

的tableView:viewForHeaderInSection:

我可以从那里同步下载数据,但这会暂时冻结UI,这根本不是用户友好的.


更多细节:

我的tableview基于Core Data,下载的数据可以存储在我的图像实体的二进制属性中.因此,我检查了是否可以检测到更改,并且有一种NSFetchedResultsControllerDelegate方法几乎可以执行我想要的操作:

控制器:didChangeSection:atIndex:forChangeType:

但遗憾的是它不支持节标题更新(仅删除/插入)...

有任何想法吗?非常感谢你的帮助.

Lin*_*ses 11

你能不能打电话:

- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation
Run Code Online (Sandbox Code Playgroud)

收到新数据并更新CoreData DB后.

  • 请注意,在您使用该部分时,这将胜过该部分中的任何其他表动画.我在这个7896719上有一个关于苹果的错误.只需要打电话刷新页眉或页脚就可以了. (3认同)

小智 5

如果您使用FetchedResultsControllers,也是一个选项:

在子程序中:

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
   atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
  newIndexPath:(NSIndexPath *)newIndexPath {
Run Code Online (Sandbox Code Playgroud)

在某处添加此行(假设您已在标题中声明了变量...):

    sectionToUpdate = newIndexPath.section;
Run Code Online (Sandbox Code Playgroud)

然后在:

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller 
Run Code Online (Sandbox Code Playgroud)

最后添加这样的东西:

if (sectionToUpdate != -1) {
    [yourtableview         beginUpdates];
    [yourtableview         reloadSections:[NSIndexSet indexSetWithIndex:sectionToUpdate] withRowAnimation:UITableViewRowAnimationFade];
    [yourtableview         endUpdates];
    sectionToUpdate = -1;
}
Run Code Online (Sandbox Code Playgroud)

应该可以避免您对表进行整个重新加载.