Objective c 核心数据和UITableView实践/问题
我对iPhone编程和CoreData都是新手。我只是想知道一些常规做法和做事方式 我正在尝试将实体从CoreData添加/加载到UITableView。因此,在AppDelegate中,我在didfinishlaunchingwithoptions上加载一个实体的NSArray(NSManagedObjects),并填充我的表视图控制器的NSArray。然后,在表视图控制器中,它使用NSManagedObjects的NSArray在CellForRowatineXpath方法中指定单元格视图 这是最好的方法吗?我应该使用一个NSManagedObjects数组来加载它并通过添加/删除来管理该数组,还是应该循环提取并填充一个新的类对象,我分别创建该类对象来表示每个单元格将包含的每个对象 我不想做比需要更多的工作,但我也不想做得太差Objective c 核心数据和UITableView实践/问题,objective-c,uitableview,ios4,core-data,ios5,Objective C,Uitableview,Ios4,Core Data,Ios5,我对iPhone编程和CoreData都是新手。我只是想知道一些常规做法和做事方式 我正在尝试将实体从CoreData添加/加载到UITableView。因此,在AppDelegate中,我在didfinishlaunchingwithoptions上加载一个实体的NSArray(NSManagedObjects),并填充我的表视图控制器的NSArray。然后,在表视图控制器中,它使用NSManagedObjects的NSArray在CellForRowatineXpath方法中指定单元格视图
请帮忙,非常感谢 您的应用程序委托只需将其
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;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
UITableViewCell*单元格=;
NSManagedObject*managedObject=[objectAtIndexPath:indexPath];
//使用托管对象中的数据配置单元格。
返回单元;
}
查看项目模板,阅读和参考以了解更多信息。苹果的文档包括一个完整的源代码示例。您的应用程序代理应该将其
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;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
UITableViewCell*单元格=;
NSManagedObject*managedObject=[objectAtIndexPath:indexPath];
//使用托管对象中的数据配置单元格。
返回单元;
}
查看项目模板,阅读和参考以了解更多信息。苹果的文档包括一个完整的源代码示例。Ah ok!我试试这个!非常感谢。啊,好的!我试试这个!非常感谢。