- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if (self.resultsController != nil) {
return;
}
iBountyHunterAppDelegate *appDelegate = (iBountyHunterAppDelegate *)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *managedObjectContext = appDelegate.managedObjectContext;
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Fugitive" inManagedObjectContext:managedObjectContext];
[request setEntity:entityDescription];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"captured == YES"];
[request setPredicate:predicate];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
[sortDescriptors release];
[sortDescriptor release];
NSFetchedResultsController *fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
managedObjectContext:managedObjectContext sectionNameKeyPath:nil
cacheName:@"captured_list.cache"];
fetchedResultsController.delegate = self;
NSError *error;
BOOL success = [fetchedResultsController performFetch:&error];
if (!success) {
// Handle error
}
self.resultsController = fetchedResultsController;
[request release];
[self.tableView reloadData];
}
Force a reload on data change: V useful:
-(void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
[self.tableView reloadData];
}
...and the code to display the data in the table:
// Number of selections allowed:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [[self.resultsController sections] count];
}
// Number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [[[self.resultsController sections] objectAtIndex:section] numberOfObjects];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
Fugitive *fugitive = [self.resultsController objectAtIndexPath:indexPath];
cell.textLabel.text = fugitive.name;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Delegate a click down to this class:
FugitiveDetailViewController *fugitiveDetailViewController = [[FugitiveDetailViewController alloc] initWithNibName:@"FugitiveDetailViewController" bundle:nil];
fugitiveDetailViewController.fugitive = [self.resultsController objectAtIndexPath:indexPath];
[self.navigationController pushViewController:fugitiveDetailViewController animated:YES];
[fugitiveDetailViewController release];
}
No comments:
Post a Comment