iPhone:获取谓词对象的indexPath

发布于 2024-09-02 22:30:28 字数 1266 浏览 2 评论 0原文

我正在使用谓词来查找核心数据中的对象。我可以成功找到所需的对象,但我还需要获取该对象的索引路径,以便我可以推送该对象的详细信息视图。目前我有以下代码用于获取我的对象:

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

揽月 2024-09-09 22:30:28

索引路径只是一组索引,例如 {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 为您处理表视图更新 - 它将为您处理索引路径,具体取决于您对部分的分区方式。

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:

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文