NSFetchedResultsController 和我的 TableView 结构

发布于 2024-09-07 02:02:54 字数 489 浏览 2 评论 0原文

我有一个包含两个部分的 tableView 控制器。第一部分有几个输入字段,并不真正显示核心数据。第二部分显示使用 Core Data 保存的数据库中的项目。

我有一个 NSFetchedResultsController ,并为 cellForRowAtIndexPathdidSelectRowAtIndexPath 提供数据,如下所示。对于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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小兔几 2024-09-14 02:02:54

执行此操作的唯一方法是将传递到各种 UITableViewDataSource/UITableViewDelegate 方法中的 NSIndexPath 对象转换为适合您的 的索引路径NSFetchedResultsController。我建议在您的类中添加一个方法来执行此操作。

此方法的返回值将与 NSFetchedResultsController 使用的节号匹配。另外,如果将来您出于某种原因最终需要第二个标题部分,则很容易调整您的方法来做到这一点。

The only way to do this is to translate the NSIndexPath objects passed into the various UITableViewDataSource/UITableViewDelegate methods into index paths appropriate for your NSFetchedResultsController. 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.

泪冰清 2024-09-14 02:02:54

为什么你需要告诉它任何事情?在委托方法中,只需将节索引偏移 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 the NSFetchedResultsController 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文