views:

418

answers:

2

I am using a fetched results controller, which, it seems wants to set up using sections in my UITableView. Well, in this case, I don't want to use sections. Which, is easy enough to set the value for numberOfSectionsInTableView: to 1. But, now I am not sure how to get the numberOfRowsInSection: to return all of the cells into one section.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
    return [sectionInfo numberOfObjects];
}

How can I make that method return all the cells, since I only want to use 1 section?

+1  A: 

In order for your fetched results controller to avoid returning objects in sections, you need to properly initialize it as follows:

NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];

Here there will be no sections because you pass nil as the argument of sectionNameKeyPath. Then, your tableView:numberOfRowsInSection method is correct.

unforgiven
Yes! Thank you very much, this worked perfectly!
Nic Hubbard
A: 

Even though you don't want to use sections, and initialize the fetchedResultsController with sectionNameKeyPath= nil, this fetchedResultsController is still populated with ONE section and all of its items. This is how Apple implement the fetchedResultsController.

So in your case, you have 1 section at the index 0, and in this section, you have all the items, starting from index 0 to the numberOfRowsInSection-1.

Hope it helps.

sfa