从 2 个实体获取 fetchedResultsController

发布于 2024-10-12 09:33:42 字数 733 浏览 3 评论 0原文

我正在学习 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 技术交流群。

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

发布评论

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

评论(1

踏雪无痕 2024-10-19 09:33:42

@Suwitcha Sugthana 在这种情况下,我建议您通过以不同的方式创建 NSFetchRequest 对象,将来自两个不同实体的数据填充到两个不同的数组中,假设它们是(myArray1 和 myArray2)。并打印您的两个数组在像这样的单元格上....

if(indexPath.row<[myArray1 count])
 {
  cell.text=[myArray1 objectAtIndex:indexPath.row];
 }
 else 
  cell.text=[myArray2 objectAtIndex:(indexPath.row-[myArray1 count])]

//myArray1 has data of contact
//myArray2 has data of catagory

您将通过此在表格上获得所需的格式....希望这可以帮助您!

@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....

if(indexPath.row<[myArray1 count])
 {
  cell.text=[myArray1 objectAtIndex:indexPath.row];
 }
 else 
  cell.text=[myArray2 objectAtIndex:(indexPath.row-[myArray1 count])]

//myArray1 has data of contact
//myArray2 has data of catagory

And you will get your required format on the table through this..........Hope this may Help u!!!

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