我有一个由 NSFetchedResultsController 驱动的 UITableView。我调用一个方法来创建一个 NSPredicate,该 NSPredicate 会过滤自今天起具有时间戳的实体。在我的 NSFetchedResultsController 中我应用了谓词。
if (appDelegate.displayTodayOnly) {
[fetchRequest setPredicate:datePredicate];
}
这一切都很好。但是,有时我需要查看“今天”之前的条目。当 TableView 已经位于表的开头时,我通过向下滑动 TableView 来完成此操作。
-(void) scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
if (self.tableView.contentOffset.y<0) {
if (appDelegate.displayTodayOnly) {
[appDelegate setDisplayTodayOnly:NO];
NSInteger resultsOffset = [[self.fetchedResultsController fetchedObjects] count];
//[self.tableView beginUpdates];
[__fetchedResultsController release];
__fetchedResultsController=nil;
NSError *error;
if (![[self fetchedResultsController] performFetch:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
exit(-1); // Fail
}
//[self.tableView endUpdates];
NSInteger fullCount = [[self.fetchedResultsController fetchedObjects] count];
if (fullCount>resultsOffset) {
NSUInteger sect = [[self.fetchedResultsController sections] count]-2;
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:sect];
NSUInteger rows = [sectionInfo numberOfObjects]-1;
NSIndexPath *indexing = [NSIndexPath indexPathForRow:rows inSection:sect];
NSLog(@"IndexPath = section:%d row:%d",sect,rows);
[self.tableView reloadData];
[self performSelector:@selector(scrollToIndexPath:) withObject:indexing afterDelay:0.0001];
}
}
}
}
这也可以,但不优雅。它丢弃 fetchedResultsController,并重新加载表,从时间开始的条目开始。然后,我让它滚动到上一个表条目上方的索引路径。
我希望发生的是在当前表的顶部之前加载和插入条目。有谁知道一种优雅的方法来改变获取以实现这种情况?
最佳答案
我通常做的是设置
self.fetchedResultsController = nil;
并重新加载。它非常高效,并且代码量最少(假设您像在 Xcode 模板中一样延迟加载和创建获取的结果 Controller )。
关于ios - NSFetchedResultsController 刷新 UITableView 条目而不会重新加载卡顿,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9170203/