开发者

How to section UITableView on Core Data relationship?

I have 2 entities - BlogEntry and BlogComments.

BlogEntry.comments is a "to many" relationship to BlogComments

| BlogEntry     |
-----------------
| subject       |
| body          |
| comments (rel)|



| BlogComments   |
------------------
| commentText    |
| blogEntry (rel)|

Now, I have a tableview that I want to be able to have the first row being the BlogEntry body (text) and the rest of the rows being 开发者_如何学Pythonthe BlogComments. Is this possible with an NSFetchedResultsController? Can I section off of a relationship like that? If so, can someone point me in the right direction?


Use a grouped UITableView along with a NSFetchedResultsController that is using a sectionNameKeyPath and an NSPredicate:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"BlogComments" 
                                          inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];

[fetchRequest setFetchBatchSize:20];

// Ensure we get a single blog entry and all it's associated comments
NSPredicate *predicate = 
                 [NSPredicate predicateWithFormat:@"blogEntryID=%@", blogEntryID];

[fetchRequest setPredicate:predicate];

NSString *key = @"blogEntry.subject";
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:key 
                                                               ascending:YES];
NSArray *sortDescriptors = @[sortDescriptor];
[fetchRequest setSortDescriptors:sortDescriptors];

NSFetchedResultsController *aFetchedResultsController = 
         [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                             managedObjectContext:managedObjectContext
                                               sectionNameKeyPath:keycacheName:nil];

aFetchedResultsController.delegate = self;

To determine the title for a section, use the fetchedResultsController's section property:

- (NSString *)tableView:(UITableView *)tableView 
               titleForHeaderInSection:(NSInteger)section 
{
   if ([[fetchedResultsController sections] count] > section)
   {
    id <NSFetchedResultsSectionInfo> sectionInfo = 
                     [[self.fetchedResultsController sections] objectAtIndex:section];

       return [sectionInfo name];
    }
    return nil;
}

To determine the number of rows in a particular section:

- (NSInteger)tableView:(UITableView *)tableView 
               numberOfRowsInSection:(NSInteger)section
{
    NSInteger numberofRows = 0;

    if ([[fetchedResultsController sections] count] > 0)
    {
        id <NSFetchedResultsSectionInfo> sectionInfo = 
                 [[fetchedResultsController sections] objectAtIndex:section];
        numberofRows = [sectionInfo numberOfObjects];
    }

    return numberofRows;
}

And finally, to get the number of sections in the tableview:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSUInteger count = [[fetchedResultsController sections] count];
    return count; 
}

The result is a tableview with 1 section representing the blog entry and all it's associated blog comments as rows in the tableView.


You probably don't want to use a NSFetchedResultsController. It is designed to easily display large numbers of the same managed object. You have a table composed of two managed objects.

(I'm assuming here that each table will display just one blog post with its comments.)

What you need to do is use a sectioned table. The first section will display the body of the blog post and the second section will display the comments. If the post itself is selected in another view then you don't need a fetch at all. You just walk the relationship between the post and its comments. Put the comments in an array and sort them however you wish.

In the numberOfSectionsInTableView: method return 2. In numberOfRowsInSection: have a switch statement. If the section is zero return 1 for the blog post. If two, return the count for the comment array. In cellForRowAtIndexPath: repeat the switch to return the proper cell for either section.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜