TableView,以正确的顺序显示核心数据 NSSet 中的记录
我已经研究这个问题有一段时间了,我认为有很多解决方案,但我不确定它们是否那么好。我可能错过了一些东西:-)
我有两张桌子。对于表 A 中的每条记录,表 B 中都有多条记录。即一对多关系。我已将其映射到核心数据中并生成了类。到目前为止,一切都很好。
我的基于表视图的 UI 需要如下所示:
Section 1
Cell 1: Table A: field1
Cell 2: Table A: field2
-----
Section 2
Cell 1: Table B: record 1: field1
Cell 2: Table B: record 2: field1
....
当我在 tableView:cellForRowAtIndexPath:
中时,很容易处理第 1 部分的单元格,因为它们是来自单个表 A 记录的不同字段。第 2 部分的单元格有所不同。每次调用 tableView:cellForRowAtIndexPath: 时,我都需要从表 A 的 NSSet 中获取正确的表 B 记录。这就是问题所在。
NSSet doco表明它不保证该订单。这意味着,如果我想要基于索引的特定记录,我有几种可能的技术:
- 将排序描述符添加到核心数据查询并使用快速枚举来获取我想要的记录。我不知道这是否有效,因为我们仍在使用 NSSet。
- 获取数据后使用排序描述符对 NSSet 进行排序以生成 NSArray 并在开始表加载之前将其存储在属性中。
- 我现在正在考虑第三个选项,即为 NSSet 编写一个装饰器,它的作用类似于 NSArray,并允许我指定排序字段并自动跟踪 NSSet 的更改。虽然很棘手,但从长远来看可能会有所回报。
这些答案似乎都不是那么好。有更好的方法吗?
I've been researching this for a while and I think there are a number of solutions, but I'm not sure they are that good. It may be that I've missed something :-)
I have two tables. For each record in TableA there are multiple records in TableB. I.e. a one to many relationship. I've mapped it out in core data and generated the classes. So far so good.
My table view based UI needs to look like this:
Section 1
Cell 1: Table A: field1
Cell 2: Table A: field2
-----
Section 2
Cell 1: Table B: record 1: field1
Cell 2: Table B: record 2: field1
....
When I'm in my tableView:cellForRowAtIndexPath:
it's easy to handle section 1 cells because they are different fields from the single Table A record. The section 2 cells are different. Each time tableView:cellForRowAtIndexPath:
is called I need to get the correct Table B record from Table A's NSSet. And thats the problem.
NSSet doco indicates that it does not garrantee the order. Which means that if I want a specific record based on an index I have several possible techniques:
- Add a sort descriptor to the core data query and use the fast enumeration to get to the record I want. I don't know if this will work because we are still working with an NSSet.
- Sort the NSSet using a sort descriptor after getting the data to generate a NSArray and store that in a property before starting the table load.
- I'm now thinking of a third option which is to write a decorator for an NSSet which acts like a NSArray and allows me to specify a sort field and automatically tracks changes to the NSSet. Tricky but might pay off in the long run.
neither of these answers seem that great. Is there a better way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您只想要对象,请使用 NSSet 访问器。
如果您希望对对象进行排序,请设置 NSFetchRequest 并添加适当的 NSSortDescriptors。
然后,只需使用
executeFetchRequest:error:
执行获取更好的是,由于这是专门针对 tableView 的,因此使用 NSFetchedResultsController 来填充表视图。这就是它的用途。
If you just want your objects, use the NSSet accessor.
If you want the object sorted, set up an NSFetchRequest and add the appropriate NSSortDescriptors.
Then, simply perform the fetch with
executeFetchRequest:error:
Even better, Since this is specifically for a tableView, use NSFetchedResultsController to populate the table view. That's what it's there for.
我最终将 NSArray 存储在控制器中并将 NSSet 记录排序到其中。
I've ended up with a NSArray being stored in the controller and sorting the NSSet records into it.