我在我的 tableview 中使用 NSFetchedResultsController
,它工作正常。我的问题是,当我的 _fetchedResultsController
有 0 行时,我想创建“无数据 View ”。
我试着在我的 numberOfSectionsInTableView:
中像这样创建它
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if ([[_fetchedResultController.sections firstObject] numberOfObjects] == 0) {
MyNoDataView *view = [[MyNoDataView alloc] initWithFrame:tableView.frame];
tableView.backgroundView = view;
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
return 0;
}
else {
tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
tableView.backgroundView = nil;
return 1;
}
}
效果很好。如果还没有插入数据,它不会显示数据 View ,如果有数据,它会显示我的表格 View 。
当我对其行进行删除操作时,我的问题就开始了。从技术上讲,我只是更新它的对象属性 (isActive
) 作为我在 fetchedResultController
中的 fetchRequest
使用这个 NSPredicate
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"isActive == %@ ", @YES];
[fetchRequest setPredicate:predicate];
然后,如果只有 1 行,我将其删除,则会出现此错误
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView internal bug: unable to generate a new section map with old section count: 1 and new section count: 0'
所以我尝试删除我的 numberOfSectionsInTableView:
,突然那些错误就消失了。我不知道这里发生了什么,但我认为我在 numberOfSectionsInTableView:
上的条件有误。
如有任何帮助,我们将不胜感激。非常感谢!
最佳答案
已解决
基于 Sneak的答案,分配“MyNoDataView”并在 numberOfSectionsInTableView 中设置 seperatorstyles 是非常糟糕的做法。所以我最终在获取的结果 Controller 委托(delegate)上修改了我的 controllerDidChangeContent:
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
// The fetch controller has sent all current change notifications, so tell the table view to process all updates.
if ([controller.fetchedObjects count]) {
self.tableView.backgroundView = nil;
}
else {
MyDataView *view = [[MyDataView alloc] initWithFrame:self.tableView.frame];
self.tableView.backgroundView = view;
}
[self.tableView endUpdates];
}
关于ios - 使用 NSFetchedResultsController 在 tableView 中没有数据 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42331684/