views:

78

answers:

1

Hi,

I need help with a little something.

I use Core Data to make this happen:

I have a main tableView where categories are stored. When one of the categories are pressed, another tableView is pushed and display that specific categories content. There is a one-to-many relationship between the category and content.

I want to display the total number of content in the categories tableview row, for each of the category. I manage to do this for just a single category by putting this code after the fetchrequest in the contents tableView controller:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *numberString = [NSString stringWithFormat:@"%i", [contentArray count]]; 
[defaults setObject: numberString forKey:kContentNumber];

And then in the category viewcontroller, I just load the number from NSUserDefault, and display it in the tableView. Of course, the number become the same for all the categories row.

My challenge is to get the specific number from each of the content, and display it to the related category.

Any help will be highly appreciated. Thanks in advance

+1  A: 

It sounds like you have a Category entity that has a to-many relationship to a Content entity.

If so, you can just ask the Content set its count from the Category's UITableViewController:

[category.contents count]

Example header:

#import <CoreData/CoreData.h>

@class Content;

@interface Category :  NSManagedObject  
{
}

@property (nonatomic, retain) NSSet* contents;

@end


@interface Category (CoreDataGeneratedAccessors)
- (void)addContentsObject:(Content *)value;
- (void)removeContentsObject:(Content *)value;
- (void)addContents:(NSSet *)value;
- (void)removeContents:(NSSet *)value;

@end
gerry3