将额外的行添加到由NSFetchedResultsController管理的UITableView

Jon*_*Cox 10 uitableview nsfetchedresultscontroller ios ios5

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 due to uncaught exception 'NSRangeException', 
reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
Run Code Online (Sandbox Code Playgroud)

知道是什么导致了这个吗?或者是否有更好的方法将额外的行添加到由NSFetchedResultsController控制的表视图中?

jrt*_*ton 13

如果您实现文档中指示的所有数据源方法(更新等),则获取的结果控制器与tableview紧密相关.它会变得相当混乱和黑客.

相反,你的"额外行"可能是表格的页脚视图吗?这总是在底部.让它看起来像一个细胞并不是太多的工作,但从它的外观来看,你希望它看起来与其他细胞看起来不同.