开发者

How do I access the right child entites when I select a parent?

This is one of those situations where I think I've solved my own problem but, being new to Cocoa (and programming in general), I'd like someone to check that I'm right!

My original problem was as follows: I have a simple CoreData program with a one-to-many relationship between two entities: "Categories" (parent) and "Checklists" (child). Tapping on a Category brings up a new view with a table of its Checklists. The problem was that if I tapped on one Category, I got a big list of all the Checklists from all the Categories - this list was the same no matter which category I tapped on.

My solution is to use a @property and a predicate. When I set up the Checklists view controller (in the Root View Controller's TableView:DidSelectRowAtIndexPath:) I did the following:

Category *category = (Category *)[__fetchedResultsController objectAt开发者_如何转开发IndexPath:indexPath];

detailViewController.category = category;

Then in the Checklist View Controller's fetchedResultsController method I did the following:

NSPredicate *requestPredicate = [NSPredicate predicateWithFormat:@"category.name = %@", self.category.name];
[fetchRequest setPredicate:requestPredicate];

This seems to have worked, but I just wanted to check I have gone about this in the right way. Thanks!

-

EDIT - here's a screenshot of my Core Data model:

How do I access the right child entites when I select a parent?


Yes, thats OK. You could also get the sorted array using NSSortDescriptor :

NSSortDescriptor *checkListName = [[NSSortDescriptor allo] initWithKey@"name" ascending:YES] autorelease];

NSArray *sortedDescArray = [NSArray arrayWithObjects: checkListName, nil];

[fetchRequest setSortDescriptors: sortedDescArray];

All the best.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜