NSFetchedResultsController 和我的 TableView 结构
我有一个包含两个部分的 tableView 控制器。第一部分有几个输入字段,并不真正显示核心数据。第二部分显示使用 Core Data 保存的数据库中的项目。
我有一个 NSFetchedResultsController ,并为 cellForRowAtIndexPath
和 didSelectRowAtIndexPath
提供数据,如下所示。对于section = 0,我手动提供适当的输入字段,对于section = 1,我想使用[fetchedResultsController objectAtIndexPath:indexPath]
。 () 但是,由于获取的结果控制器只知道一个部分,因此这是行不通的。
我知道我可以创建一个新的 IndexPath,其部分 = 0,然后将其提供给 NSFetchedResultsController。这是首选解决方案还是有其他方法来“告诉”NSFetchedResultsController 期望什么?
I have a tableView controller with two sections. The first section has a couple of input fields and is not really displaying core data. The second section displays items from a database saved with Core Data.
I have an NSFetchedResultsController
and I serve up data for cellForRowAtIndexPath
and didSelectRowAtIndexPath
as follows. For section = 0, I manually serve up the appropriate input fields, and for section = 1 I want to use [fetchedResultsController objectAtIndexPath:indexPath]
. () However, since the fetched results controller only knows about one section, this doesn't work.
I know I can create a new IndexPath with section = 0, and then feed that to the NSFetchedResultsController
. Is that the preferred solution or is there another way to 'tell' the NSFetchedResultsController
what to expect?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
执行此操作的唯一方法是将传递到各种
UITableViewDataSource
/UITableViewDelegate
方法中的NSIndexPath
对象转换为适合您的的索引路径NSFetchedResultsController
。我建议在您的类中添加一个方法来执行此操作。此方法的返回值将与 NSFetchedResultsController 使用的节号匹配。另外,如果将来您出于某种原因最终需要第二个标题部分,则很容易调整您的方法来做到这一点。
The only way to do this is to translate the
NSIndexPath
objects passed into the variousUITableViewDataSource
/UITableViewDelegate
methods into index paths appropriate for yourNSFetchedResultsController
. I'd recommend adding a method to your class that does this.The return values from this method will match the section numbers the
NSFetchedResultsController
uses. Also, if in the future you end up needing a second header section for whatever reason, it's easy enough to adjust your method to do that.为什么你需要告诉它任何事情?在委托方法中,只需将节索引偏移 1 就可以了。
您可以创建自己的
NSIndexPath
实例以传递到NSFetchedResultsController
来解决此问题。更新
如果您想有两个部分,那么是的,这是正确的答案。不过,我会考虑将您的输入字段放入表头而不是部分中。在我看来,这将是一个更清晰的答案。
Why would you need to tell it anything? In the delegate methods, just offset the section index by one and you should be fine.
You can create your own
NSIndexPath
instance to pass into theNSFetchedResultsController
to resolve this issue.Update
If you want to have two sections then yes that is the right answer. However I would consider putting your input fields into the table header instead of a section. That would be a cleaner answer in my opinion.