【问题标题】:Core Data and UITableView Practices/Questions核心数据和 UITableView 实践/问题
【发布时间】:2012-01-29 05:49:13
【问题描述】:

我对 iPhone 编程和 CoreData 都是新手。我只是想知道一些一般做法和做事方式。

我正在尝试将实体从 CoreData 添加/加载到 UITableView。因此,在 AppDelegate 中,我在 didfinishlaunchingwithoptions 上加载实体的 NSArray (NSManagedObjects),并为我的表视图控制器填充 NSArray。在表格视图控制器中,它使用 NSManagedObjects 的 NSArray 在 cellForRowAtIndexPath 方法中指定单元格视图。

这是最好的方法吗?我应该使用 NSManagedObjects 数组来加载它并使用添加/删除来管理该数组,还是应该循环访问并填充我单独创建的新类对象以表示每个单元格将包含的每个对象?

我不想做超出需要的工作,但我也不想做任何糟糕的事情。

请帮忙,非常感谢!

【问题讨论】:

    标签: objective-c uitableview ios4 core-data ios5


    【解决方案1】:

    您的应用程序委托应该将其NSManagedObjectContext 传递给您的表格视图控制器,然后它会创建自己的NSFetchedResultsController 实例,该类可以有效地加载托管对象以在UITableView 中显示并响应您的更改对象图。

    Xcode 4.2 中的“Master-Detail Application”核心数据项目模板使用了这种模式。这是一个很好的参考和起点。以下是主表视图控制器延迟加载和配置其结果控制器的方式:

    - (NSFetchedResultsController *)fetchedResultsController
    {
        if (__fetchedResultsController != nil) {
            return __fetchedResultsController;
        }
    
        // Set up the fetched results controller.
        // Create the fetch request for the entity.
        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        // Edit the entity name as appropriate.
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:self.managedObjectContext];
        [fetchRequest setEntity:entity];
    
        // Set the batch size to a suitable number.
        [fetchRequest setFetchBatchSize:20];
    
        // Edit the sort key as appropriate.
        NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:NO];
        NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];
    
        [fetchRequest setSortDescriptors:sortDescriptors];
    
        // Edit the section name key path and cache name if appropriate.
        // nil for section name key path means "no sections".
        NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Master"];
        aFetchedResultsController.delegate = self;
        self.fetchedResultsController = aFetchedResultsController;
    
        NSError *error = nil;
        if (![self.fetchedResultsController performFetch:&error]) {
            /*
             Replace this implementation with code to handle the error appropriately.
    
             abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 
             */
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        }
    
        return __fetchedResultsController;
    }
    

    一旦有了NSFetchedResultsController,只需将获取的对象插入到表格视图数据源方法中,例如:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        UITableViewCell *cell = <#Get the cell#>;
        NSManagedObject *managedObject = [<#Fetched results controller#> objectAtIndexPath:indexPath];
        // Configure the cell with data from the managed object.
        return cell;
    }
    

    查看项目模板并阅读NSFetchedResultsController classNSFetchedResultsControllerDelegate protocol 参考以了解更多信息。 Apple 的文档包含一个完整的源代码示例。

    【讨论】:

    • 啊好吧!我试试这个!谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-11
    • 2012-09-15
    • 1970-01-01
    • 1970-01-01
    • 2021-12-06
    • 1970-01-01
    • 2011-03-10
    相关资源
    最近更新 更多