UITableView,其中包含由核心数据驱动的部分

时间:2013-06-18 05:56:43

标签: ios core-data

我有以下json文件

 {
   items = ({“text1” = “lorem ipsum”;},
            {“text2” = “lorem ipsum”;});
   id = 0;
   title = “item0”;
 },
 {
   items = ({“text1” = “lorem ipsum”;},
            {“text2” = “lorem ipsum”;});
   id = 1;
   title = “item1”;
 }

我正在使用核心数据进行持久化标题是第一个实体的名称也是表视图的部分名称我希望将项目数组填充到部分例如section title =“items1”包含2个项目text1和text2 my问题在于细胞如何将这些信息添加到细胞中但是有核心数据。我首先尝试创建一个具有可转换类型的实体,例如在 NSManagedObject

内部
self.title = [dictionary objectForKey:@"title"];

NSArray *items = [dictionary objectForKey:@"items"];

if ([items isKindOfClass:[NSArray class]])
{
   for (NSDictionary *item in items)
   {
      self.arr = [clip objectForKey:@"text1"];
   }
}

关于这个主题的任何想法?

2 个答案:

答案 0 :(得分:1)

我认为你可以使用NSFetchedResultsController。查看Apple Docs了解详情。

答案 1 :(得分:0)

我正在使用以下代码获取数据

- (NSFetchedResultsController *)fetchedResultsController
{
    if (!_fetchedResultsController)
    {

        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"List" inManagedObjectContext:self.managedObjectContext];
        [fetchRequest setEntity:entity];


        NSSortDescriptor *authorDescriptor = [[NSSortDescriptor alloc] initWithKey:@"test" ascending:YES];
        NSSortDescriptor *titleDescriptor = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES];
        NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:authorDescriptor, titleDescriptor, nil];
        [fetchRequest setSortDescriptors:sortDescriptors];


        _fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:self.fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:self.sectionNameKeyPath cacheName:self.cacheName];

        _fetchedResultsController.delegate = self;
        [_fetchedResultsController performFetch:nil];
    }
    return _fetchedResultsController;
}