views:

21

answers:

1

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 the 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?

A: 

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.

TechZen
After a lot of trial and error, I decided this was the best way to go also. I left the post here for a second opinion. Thanks!
Tom