我UITableViewController在我的应用程序中使用了一个表,并且我添加了一个NSFetchedResultsController用于提供要在表中显示的数据(设置self为它的委托).
但是我想添加一个唯一的单元格作为表格的最后一个单元格,与NSFetchedResultsController谓词生成的项目无关,我希望这个单元格始终位于表格的底部.
我试过在表视图数据源中简单地将这些方法添加1:
- (NSUInteger)numberOfSectionsInTableView:(UITableView *)sender
{
return [[self.fetchedResultsController sections] count] + 1;
}
- (NSUInteger)tableView:(UITableView *)sender numberOfRowsInSection:(NSUInteger)section
{
return [[[self.fetchedResultsController sections] objectAtIndex:section] numberOfObjects] + 1;
}
Run Code Online (Sandbox Code Playgroud)
然后捕获这样生成这个额外行的情况(该表只有1个部分):
- (UITableViewCell *)tableView:(UITableView *)sender
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == [self.fetchedResultsController.fetchedObjects count]) {
//Generate the last cell in table.
} else {
//Generate the rest of the cells like normal.
}
return nil; //To keep the compiler happy.
}
Run Code Online (Sandbox Code Playgroud)
这将检查索引是否是最后一个单元格并正确处理它.
但是,我仍然在运行时收到以下错误:
*** Terminating app …Run Code Online (Sandbox Code Playgroud)