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

查看:20
本文介绍了向 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 紧密相关.它会变得非常混乱和hacky.

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天全站免登陆