Database Reference
In-Depth Information
have a sectionNameKeyPath set on the NSFetchedResultsController , there will be either
zero or one section in that array. In previous versions of iOS (prior to iOS 4. x ),
the UITableView did not like being told there were zero sections. You may run
across older code that checks the section count and always returns a minimum
of one section (with zero rows). That issue has been addressed, and the
associated check is no longer needed.
RecipesV1/PPRecipes/PPRMasterViewController.m
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
NSArray *sections = [[self fetchedResultsController] sections];
id <NSFetchedResultsSectionInfo> sectionInfo = nil;
sectionInfo = [sections objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
The tableView:numberOfRowsInSection: method is slightly more complex. Here, we
grab the array of sections, but we also grab the object within the array that
is at the index being passed into the method. There is no need to check to
see whether the index is valid since the -numberOfSectionsInTableView: method is
the basis for this index. The object that is in the NSArray is undetermined but
guaranteed to respond to the NSFetchedResultsSectionInfo protocol. One of the
methods on that protocol is numberOfObjects , which we use to return the number
of rows in the section.
RecipesV1/PPRecipes/PPRMasterViewController.m
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
NSManagedObject *object = nil;
object = [[self fetchedResultsController] objectAtIndexPath:indexPath];
cell = [tableView dequeueReusableCellWithIdentifier:@ "Cell" ];
[[cell textLabel] setText:[object valueForKey:@ "name" ]];
return cell;
}
In the -tableView:cellForRowAtIndexPath: method, we use another very useful ability
of the NSFetchedResultsController : the -objectAtIndexPath: method. With this method,
we can retrieve the exact object we need to work with in a single call. This
reduces the complexity of our -tableView:cellForRowAtIndexPath: method significantly.
 
 
 
Search WWH ::




Custom Search