2010-05-28 123 views
1

我正在使用谓词在核心数据中查找对象。我可以成功找到我想要的对象,但我还需要获取该对象的indexPath,以便可以为该对象推送一个详细信息视图。目前,我有一个让我的对象下面的代码:iPhone:获取Predicate对象的indexPath

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
    [fetchRequest setEntity:[NSEntityDescription entityForName:@"Ride" inManagedObjectContext:self.managedObjectContext]]; 
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"title = %@ AND addressFull = %@", view.annotation.title, view.annotation.subtitle]; 
    [fetchRequest setPredicate:predicate]; 
    NSMutableArray *sortDescriptors = [NSMutableArray array]; 
    [sortDescriptors addObject:[[[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES] autorelease]]; 
    [sortDescriptors addObject:[[[NSSortDescriptor alloc] initWithKey:@"addressFull" ascending:YES] autorelease]]; 
    [fetchRequest setSortDescriptors:sortDescriptors]; 
    [fetchRequest setReturnsObjectsAsFaults:NO]; 
    [fetchRequest setPropertiesToFetch:[NSArray arrayWithObjects:@"title", @"addressFull", nil]]; 
    NSError *error = nil; 
    NSArray *fetchedItems = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error]; 
    // Sohow what record we returned 
    NSLog(@"%@",[fetchedItems objectAtIndex:0]); 

所以,我可以正确得到我的对象到一个数组。 但是,如何将该对象转换为indexPath?

回答

2

索引路径只是一组索引,例如, {0, 2}可能代表一个索引路径,指向表视图的第一部分和第三行 - 假设您的数组数据的表视图表示是您的最终目标。

该索引路径可能指向数组中的任何特定对象,具体取决于将路径转换为数组索引的方式。

如果你想任意索引路径,这是很容易:

NSUInteger myFirstIndex = 0; 
NSUInteger mySecondIndex = 2; 
NSUInteger myIndices[] = {myFirstIndex, mySecondIndex}; 
NSIndexPath *myIndexPath = [[NSIndexPath alloc] initWithIndexes:myIndices length:2]; 
// ...do someting with myIndexPath... 
[myIndexPath release]; 

所以,你需要做的是找出你的阵列结构如何转化为章节和行(再次什么,假设你想制作表格视图表示)。

另一种选择是使用NSFetchedResultsController来为您处理表视图更新 - 它将为您处理索引路径,具体取决于您如何对部分进行分区。

+0

是的,你是对的,我想在UITableView中展示它。只需要在细节视图中滑动单击的地图注记。 – 2010-05-28 01:45:09

+0

我建议你把你的数据提取到一个'NSFetchedResultsController'中,让它执行表视图更新。它消除了将核心数据获取结果连接到表视图的大约99%的工作。 – 2010-05-28 01:48:21

+0

我得到它的工作,只需要用我的根控制器初始化。 – 2010-05-28 02:14:45