将额外的行添加到由NSFetchedResultsController管理的UITableView [英] Add extra row to a UITableView managed by NSFetchedResultsController

查看:97
本文介绍了将额外的行添加到由NSFetchedResultsController管理的UITableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用中为一个表使用了 UITableViewController ,我添加了 NSFetchedResultsController 到提供要在表格中显示的数据(设置 self 作为其委托)。

I'm using a UITableViewController for a table in my app, and I've added an NSFetchedResultsController to provide the data to show in the table (setting self as it's delegate).

但是我想添加作为表的最后一个单元格的唯一单元格,与 NSFetchedResultsController 的谓词生成的项目无关,我希望此单元格始终位于表格的底部。

However I would like to add a unique cell as the last cell of the table, unrelated to the items produced by the NSFetchedResultsController's predicate, I want this cell to always be at the bottom of the table.

我尝试在表格视图数据源中简单地将这些方法添加1:

I've tried simply added 1 to these methods in the table view data source:

- (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;
}

然后捕获这样生成这个额外行的情况(表只有1个部分):

And then catching the case where this extra row is being generated like so (the table only has 1 section):

- (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.
}

这会检查索引是否是最后一个单元格并正确处理它。

This checks to see if the index is the last cell and deals with it appropriately.

但是我在运行时仍然收到以下错误:

However I am still getting the following error at runtime:

*** Terminating app due to uncaught exception 'NSRangeException', 
reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'

知道造成这种情况的原因是什么?或者是否有更好的方法将额外的行添加到由NSFetchedResultsController控制的表视图中?

Any idea what's causing this? Or is there a better way of adding an extra row to a table view controlled by an NSFetchedResultsController?

推荐答案

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

The fetched results controller is pretty tightly tied to the tableview, if you implement all the datasource methods as indicated in the documentation (the updates and so on). It will get pretty messy and hacky.

您的额外行可能是表格的页脚视图吗?这将始终位于底部。让它看起来像一个细胞并不是太多的工作,但从它的外观来看,你希望它看起来与其他细胞看起来不同。

Could your "extra row" be the footer view of the table instead? This will always be at the bottom. It wouldn't be too much work to make it look like a cell, though from the look of it you want it to look different to the other cells anyway.

这篇关于将额外的行添加到由NSFetchedResultsController管理的UITableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆