从 2 个实体获取 fetchedResultsController
我正在学习 CoreData 并开始在我的一个项目中使用它。
我使用 fetchedResultsController 来从 CoreData 中获取数据来填充 uitableview 就可以了。
我遇到的问题是我需要从 2 个实体填充 1 个表。 这就是我现在所拥有的,
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Contact" inManagedObjectContext:delegate.managedObjectContext];
[fetchRequest setEntity:entity];
这只会从“联系人”实体获取数据,但我还需要使用“类别”实体的数据填充表。我不知道该怎么做。
基本上是最终结果,我想让 tableview
显示类似的内容 (假设联系人有 3 个,类别有 2 项)
uiTableindex0 : fetchContact 0 uiTableindex1 : fetchContact 1 uiTableindex2 : fetchContact 2 uiTableindex3 : fetchCategory 1 uiTableindex4 : fetchCategory 2
I am learning about CoreData and started to use it in one of my project.
I use fetchedResultsController
to get data out of CoreData to populate a uitableview
just fine.
The problem I am having is that I need to populate 1 table from 2 Entities.
This is what I have right now
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Contact" inManagedObjectContext:delegate.managedObjectContext];
[fetchRequest setEntity:entity];
This will only get data from "Contact" Entity but I also need to populate table with data from "Category" Entity as well. And I don't know how to do that.
basically the end result, I would like to have tableview
show something like
(assume contact has 3 and category has 2 items)
uiTableindex0 : fetchContact 0 uiTableindex1 : fetchContact 1 uiTableindex2 : fetchContact 2 uiTableindex3 : fetchCategory 1 uiTableindex4 : fetchCategory 2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
@Suwitcha Sugthana 在这种情况下,我建议您通过以不同的方式创建
NSFetchRequest
对象,将来自两个不同实体的数据填充到两个不同的数组中,假设它们是(myArray1 和 myArray2)。并打印您的两个数组在像这样的单元格上....您将通过此在表格上获得所需的格式....希望这可以帮助您!
@Suwitcha Sugthana in this condition i would suggest you to populate the data from two different entities into two different arrays suppose they are are (myArray1 and myArray2) by making there
NSFetchRequest
objects differently .And print your two arrays on the cell like this....And you will get your required format on the table through this..........Hope this may Help u!!!