Mic*_*chs 6 uitableview uikit nsfetchedresultscontroller ios
我正在使用一个NSFetchedResultsController刷新表视图的数据.数据本身是通过在后台运行的XML解析器提供的.解析器完成后,它会将数据保存到自己的上下文中.在NSFetchedResultsController高达立即这些变化镐和开始调用-(void)controller:didChangeObject:atIndexPath:forChangeType:newIndexPath:每个更新元素的委托方法.这也很快,在日志文件中看起来完全正常.
但是,在-(void)controllerDidChangeContent:我打电话UITableView的时候-(void)endUpdates.然后我在屏幕上看到更新动画,但在所有单元格中,在最后一个只有一半可见的单元格旁边,唯一可见的是单元格左侧的图像.所有文字标签都不可见.它需要大约5到10秒,然后所有标签都可见.
但是,如果我忽略了所有的委托电话NSFetchedResultsController,并简单地调用[self.tableView reloadData]就-(void)controllerDidChangeContent:一切正常,没有任何问题.内容立即存在.
有谁知道我在这里做错了什么?分析器显示主线程基本上什么都不做.除了调度到表视图的事件之外,还可以正确处理触摸事件.这些都没有处理.看起来表格视图正忙于做一些认真的工作,但我真的不知道那可能是什么,因为动画已经完成了.
这是我的实现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];
}
Run Code Online (Sandbox Code Playgroud)
这是我的单元格布局的代码:
- (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;
}
Run Code Online (Sandbox Code Playgroud)
这个细胞非常基本的更新:
- (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);
}
Run Code Online (Sandbox Code Playgroud)
占位符字符串也未显示.标签完全是空的.只有图像可见!
让我们关注这段代码:
case NSFetchedResultsChangeUpdate:
[(NewsItemCell*)[tableView cellForRowAtIndexPath:indexPath] updateWithNews:[self.fetchedResultsController objectAtIndexPath:indexPath]];
break;
Run Code Online (Sandbox Code Playgroud)
为了引导您了解正在发生的事情:
我的意思是:tableView命令可以将更改捆绑到一个事务中.看起来您重新退回单元格的调用最终会在下一次交易中结束.所以用上面的代码替换:
case NSFetchedResultsChangeUpdate:
[tableView reloadRowsAtIndexPaths: [NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic ];
break;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2379 次 |
| 最近记录: |