有问题的应用程序可以让用户将项目标记为收藏。当用户没有保存收藏夹时,我想通知他们这个事实(主要是我讨厌空白 tableView 的想法)。
numberOfRowsInSection
当没有用户标记为收藏的项目时,我的为零。我想设置 cell.textLabel.text = @"You have no favourites" 但是当没有表格的项目时cellForRowAtIndexPath
不会被调用。
我可以numberOfRowsInSection
在遇到 0 时测试给出结果,然后在其中测试 1 行,cellForRowAtIndexPath
然后插入自定义文本,但是如果他们只有一个最喜欢的怎么办?
更新
我尝试实现我上面的想法,并在下面推荐,但也许我做得不对,可能是因为它是一个 fetchedResultsController ,它具有在发生更改时更新表的委托方法。
当我在表格中只有一个单元格时删除单元格时出现此错误:
*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1447.6.4/UITableView.m:976
Serious application error. An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:. Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (1), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted). with userInfo (null)
并且当首先没有要显示的单元格时,它会崩溃:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[_PFBatchFaultingArray objectAtIndex:]: index (0) beyond bounds (0)'
以下是相关代码:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
id <NSFetchedResultsSectionInfo> sectionInfo = [[_fetchedResultsController sections] objectAtIndex:section];
if ([sectionInfo numberOfObjects]==0) {
return 1;
} else {
return [sectionInfo numberOfObjects];
}
}
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
if ([[_fetchedResultsController sections] objectAtIndex:0]==0) {
cell.textLabel.text = @"You have no favorites";
} else {
Shows *show = [_fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = show.ShowsToSerials.serialName;
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@", show.showTitle];
}
}