iPhone:获取谓词对象的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]);
因此,我可以正确地将我的对象放入数组中。 但是如何将该对象转换为索引路径?
I am using a predicate to find an object in core data. I can successfully find the object that I want, but I need to also get the indexPath of that object, so that I can push a details view in for that object. Currently I have the following code for getting my object:
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]);
So, I can correctly get my object into an array. But how do I translate that object into an indexPath?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
索引路径只是一组索引,例如
{0, 2}
可能表示指向表视图的第一部分和第三行的索引路径 - 假设表视图表示数组数据是你的最终目标。该索引路径可以指向数组中的任何特定对象,具体取决于您如何将路径转换为数组的索引。
如果您想创建任意索引路径,这非常简单:
因此您需要做的是弄清楚数组结构如何转换为节和行(再次假设您想创建表视图表示)。
另一种选择是使用
NSFetchedResultsController
为您处理表视图更新 - 它将为您处理索引路径,具体取决于您对部分的分区方式。An index path is just a set of indices, e.g.
{0, 2}
might represent an index path pointing at the first section and third row of a table view — assuming a table view representation of your array data is your ultimate goal.That index path could point to any particular object in your array, depending on how you translate the path to an index of your array.
If you want to make an arbitrary index path, it's pretty easy:
So what you need to do is figure out how your array structure translates to sections and rows (again, assuming you want to make a table view representation).
Another option is to use an
NSFetchedResultsController
to handle table view updates for you — it will handle index paths for you, depending on how you partition your sections.