核心数据和UITableView实践/问题 [英] Core Data and UITableView Practices/Questions

查看:112
本文介绍了核心数据和UITableView实践/问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Im new with iPhone programming and for that matter CoreData. I was just wondering about some general practices and ways of doing things.

我试图添加/加载实体从CoreData到UITableView。所以在AppDelegate我加载一个NSArray实体(NSManagedObjects)在didfinishlaunchingwithoptions和填充NSArray为我的表视图控制器。在表视图控制器中,然后使用NSManagedObjects的NSArray在cellForRowAtIndexPath方法中指定单元格视图。

I am trying to add/load entities from CoreData to a UITableView. So in the AppDelegate I am loading an NSArray of entities (NSManagedObjects) on didfinishlaunchingwithoptions and populating the NSArray for my table view controller. In the table view controller it is then using the NSArray of NSManagedObjects to specify the cell view in the cellForRowAtIndexPath method.

这是最好的方法吗?我应该使用一个NSManagedObjects数组来加载和管理该数组与添加/删除或应该循环通过并填充一个新的类对象,我创建的单独代表每个单元格将包含的每个对象的类对象?

Is this the best way to do this? Should I be using an array of NSManagedObjects to load this and manage that array with adds/deletes or should the fetch be looped through and populate a new class object that I created seperate to represent each object that each cell will contain?

我不想做比所需更多的工作,但我也不想做任何糟糕的工作。

I dont want to do more work than needed, but I dont want to do anything poorly either.

请帮助,非常感谢!

推荐答案

您的应用程序委托应该通过 NSManagedObjectContext 到您的表视图控制器,然后创建自己的实例 NSFetchedResultsController ,这是一个有效地加载显示在

Your app delegate should just pass its NSManagedObjectContext to your table view controller, which then creates its own instance of NSFetchedResultsController, a class that efficiently loads managed objects for display in a UITableView and responds to changes in your object graph.

Xcode 4.2中的主 - 详细应用程序核心数据项目模板使用此模式。UITableView 这是一个很好的参考和起点。下面是主表视图控制器如何加载和配置其结果控制器的方式:

The "Master-Detail Application" Core Data project template in Xcode 4.2 uses this pattern. It's a good reference and starting point. Here's how the master table view controller lazily loads and configures its results controller:

- (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 ,它只是将获取的对象插入表视图数据源方法的问题,例如:

Once you have an NSFetchedResultsController, it's just a matter of plugging the fetched objects into the table view data source methods, for example:

- (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类 NSFetchedResultsControllerDelegate协议参考以了解详情。 Apple的文档包括一个完整的源代码示例。

Take a look at the project template and read both the NSFetchedResultsController class and NSFetchedResultsControllerDelegate protocol references to learn more. Apple's documentation includes a complete source code example.

这篇关于核心数据和UITableView实践/问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆