I have been at this for 5 days now, and I am now at the point where I just need a solution! I have been reading and trying to understand this, and I am failing. Can anyone please help me with the solution, and walk me through it please?!
I can get the search bar to appear, but how can I get the table to reload with the right data after clicking
search?
I have tried SO many methods from adding the search as an IBOutlet, but the table is loaded all in code so there is a disconnect.
I know there is already a FRC here, aand methods to detect changes in the data, but how can I utilize this functionality?
I have a feeling this is REALLY simple and I am just new enough to this to be overlooking the obvious.
Help me Codey C Objecty, youre my only hope!
Rob
header:
Code:
@class Recipe;
@class RecipeTableViewCell;
@interface RecipeListTableViewController : UITableViewController <RecipeAddDelegate, RecipeSearchDelegate, NSFetchedResultsControllerDelegate, UISearchBarDelegate, UIAlertViewDelegate> {
@private
NSManagedObjectContext *managedObjectContext;
NSFetchedResultsController *fetchedResultsController;
}
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;
- (void)showRecipe:(Recipe *)recipe animated:(BOOL)animated;
- (void)configureCell:(RecipeTableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath;
@end
implementation:
Code:
#import "RecipeListTableViewController.h"
#import "RecipeDetailViewController.h"
#import "Recipe.h"
#import "RecipeTableViewCell.h"
@implementation RecipeListTableViewController
@synthesize fetchedResultsController, managedObjectContext;
#pragma mark -
#pragma mark UIViewController overrides
- (void)viewDidLoad {
// Configure the navigation bar
self.title = @"Sooper Recipes!";
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UISearchBar *destinationSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,170,320,44)];
[[self tableView] setTableHeaderView:destinationSearchBar];
self.tableView.rowHeight = 44.0;
// error code removed to shorten
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Support all orientations except upside down
return YES;//(interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (void)showRecipe:(Recipe *)recipe animated:(BOOL)animated {
// push detail view code // this works fine
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSInteger numberOfRows = 0;
if ([[fetchedResultsController sections] count] > 0) {
id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
//NSLog(@"sectionInfo : %@", sectionInfo);
//NSLog(@"sections : %i", [[fetchedResultsController sections] count]);
numberOfRows = [sectionInfo numberOfObjects];
}
return numberOfRows;
}
//---------------------------------------!@#
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSInteger numberOfRows = 0;
if ([[fetchedResultsController sections] count] > 0) {
id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
numberOfRows = [sectionInfo numberOfObjects];
//NSLog(@"numberOfObjects : %i", [sectionInfo numberOfObjects]);
return sectionInfo.name;
}
return 0;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [[fetchedResultsController sections] count];//return 6;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Dequeue or if necessary create a RecipeTableViewCell, then set its recipe to the recipe for the current row.
static NSString *RecipeCellIdentifier = @"RecipeCellIdentifier";
RecipeTableViewCell *recipeCell = (RecipeTableViewCell *)[tableView dequeueReusableCellWithIdentifier:RecipeCellIdentifier];
if (recipeCell == nil) {
recipeCell = [[[RecipeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:RecipeCellIdentifier] autorelease];
recipeCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
[self configureCell:recipeCell atIndexPath:indexPath];
//NSLog(@"%i",[indexPath intValue]);
return recipeCell;
}
- (void)configureCell:(RecipeTableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
// Configure the cell
Recipe *recipe = (Recipe *)[fetchedResultsController objectAtIndexPath:indexPath];
cell.recipe = recipe;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
Recipe *recipe = (Recipe *)[fetchedResultsController objectAtIndexPath:indexPath];
//NSLog(@"didSelectRowAtIndexPath");
[self showRecipe:recipe animated:YES];
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the managed object for the given index path
NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];
[context deleteObject:[fetchedResultsController objectAtIndexPath:indexPath]];
// Save the context.
NSError *error;
if (![context save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
}
- (NSFetchedResultsController *)fetchedResultsController {
if (fetchedResultsController == nil) {
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Recipe" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"category" ascending:YES];// was name
NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
//NSLog(@"NSInteger value :%@", sortDescriptor);
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor,sortDescriptor2, nil];// was 2
[fetchRequest setSortDescriptors:sortDescriptors];
// Edit the section name key path and cache name if appropriate.
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:@"state" cacheName:@"Root"];//@"state"
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
[aFetchedResultsController release];
[fetchRequest release];
[sortDescriptor release];
[sortDescriptor2 release];
[sortDescriptors release];
}
//?NSLog(@"NSInteger value :%i", numberOfRows);
return fetchedResultsController;
}
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
// The fetch controller is about to start sending change notifications, so prepare the table view for updates.
[self.tableView beginUpdates];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
UITableView *tableView = self.tableView;
switch(type) {
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
[self configureCell:(RecipeTableViewCell *)[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
break;
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {
switch(type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
// The fetch controller has sent all current change notifications, so tell the table view to process all updates
[self.tableView endUpdates];
}
- (void)dealloc {
[fetchedResultsController release];
[managedObjectContext release];
[super dealloc];
}
@end