title: how to add search bar with core data
body: <p>i have tried all solution from stackoverflow.but no use ...
  i have one entity called "Notes " .
  i need to add search bar to search the thing in my table view...  </p>
  
  <pre><code>@interface ViewController ()
  
  {
      NSDateFormatter *formatter;
      BOOL isChecked;   
       NSArray *searchResults;
  }
  
  @property (nonatomic, strong) UISearchBar *searchBar;
  @property (nonatomic, strong) UISearchDisplayController *searchController;
  
  @property (nonatomic) BOOL *isChecked;
  
  @property (strong) NSMutableArray *notes;
  
  @end
  
  
  @implementation ViewController
  
  @synthesize tableView;
  
  @synthesize addButton;
  
  @synthesize managedObjectContext;
  
  - (NSManagedObjectContext *)managedObjectContext {
  
      NSManagedObjectContext *context = nil;
  
      id delegate = [[UIApplication sharedApplication] delegate];
  
      if ([delegate performSelector:@selector(managedObjectContext)]) {
  
          context = [delegate managedObjectContext];
  
      }
  
      return context;
  
  }
  
  - (void)viewDidLoad {
  
      [super viewDidLoad];
  
      self.navigationItem.title = @"My Notes";
      tableView.dataSource = self;
  
      tableView.delegate = self;
  
      [self.view addSubview:tableView];
  
      formatter = [[NSDateFormatter alloc] init];
  
      formatter.doesRelativeDateFormatting = YES;
  
      formatter.locale = [NSLocale currentLocale];
  
      formatter.dateStyle = NSDateFormatterShortStyle;
  
      formatter.timeStyle = NSDateFormatterNoStyle;
  
           CATransition *animation = [CATransition animation];
  
              [animation setDuration:2.0];
  
              [animation setType:kCATransitionPush];
  
          [animation setSubtype:kCATransitionFromTop];
  
          [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];
  
          [[addButton layer] addAnimation:animation forKey:@"SwitchToDown"];
  
      // place search bar coordinates where the navbar is position - offset by statusbar
  
      self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 20, 320, 44)];
  
  
      UIBarButtonItem *searchButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(toggleSearch)];
  
      self.navigationItem.rightBarButtonItem = searchButton;
  
      self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
  
      self.searchController.searchResultsDataSource = self;
  
      self.searchController.searchResultsDelegate = self;
  
      self.searchController.delegate = self;
  
  }
  
  - (void)viewDidAppear:(BOOL)animated
  
  {
      [super viewDidAppear:animated];
  
      NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
  
      NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Notes"];
  
      NSError *error = nil;
  
      self.notes = [[managedObjectContext executeFetchRequest:fetchRequest error:&amp;error] mutableCopy];
  
      NSSortDescriptor *titleSorter= [[NSSortDescriptor alloc] initWithKey:@"mod_time" ascending:NO];
  
      [self.notes sortUsingDescriptors:[NSArray arrayWithObject:titleSorter]];
  
      NSLog(@"Your Error - %@",error.description);
  
      [tableView reloadData];
  
  }
  
  #pragma mark - Search controller
  
  - (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller
  {
  }
  
  - (void) searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller
  
  {
     // reposition the table where we want after search has completed
  
      // need to reschedule on runloop to rejig the table layout and correct the
  
      // offset and insets given no search bar will be displayed at the top of the controller
  
  //    // as we remove it
  
  //    dispatch_async(dispatch_get_main_queue(), ^{
  
  //        self.tableView.contentInset = UIEdgeInsetsMake([self.topLayoutGuide length], 0, 0, 0);
  
  //        self.tableView.contentOffset = CGPointMake(0, -[self.topLayoutGuide length]);
  
  //    });
  
  }
  
  - (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller
  {
  
  //    [self.searchBar removeFromSuperview];
  
  }
  
  - (void)toggleSearch{
  
      [self.view addSubview:self.searchBar];
  
      [self.searchController setActive:YES animated:YES];
  
      [self.searchBar becomeFirstResponder];
  
  }
  
  - (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
  {
      NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
  
  
  
      NSEntityDescription *entity = [NSEntityDescription
  
                                     entityForName:@"Notes" inManagedObjectContext:_notes];
  
      [fetchRequest setEntity:entity];
  
      NSPredicate *predicate = [NSPredicate predicateWithFormat:@"notes.title contains[c] %@", searchText];
  
      [fetchRequest setPredicate:predicate];
  
      NSError *error;
  
      NSArray* searchResults = [managedObjectContext executeFetchRequest:fetchRequest error:&amp;error];
  
  }
  
  -(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
  {
      [self filterContentForSearchText:searchString
  
                                 scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
  
                                        objectAtIndex:[self.searchDisplayController.searchBar
  
                                                       selectedScopeButtonIndex]]];
  
      return YES;
  
  }
  
  #pragma mark - Table view data source
  
  - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  
  {
  
      // Return the number of sections.
  
      return searchResults.count;
  
  }
  
  - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  
  {
  
      if (tableView == self.searchDisplayController.searchResultsTableView) {
  
          return [searchResults count];    
  
      } else {
  
        return self.notes.count;
  
  }
  
  }
  
  - (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  
  {
  
      TableViewCell *cell = (TableViewCell*)[aTableView dequeueReusableCellWithIdentifier:@"MycellIdentifier"];
      if(cell == nil)
  
      {
          cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MycellIdentifier"];
      }
  
         // Configure the cell...
  
      NSManagedObject *note = [self.notes objectAtIndex:indexPath.row];
  
      NSDate *date = [note valueForKey:@"mod_time"];
  
      NSString *dateString = [formatter stringFromDate:date];
  
      cell.textLabel.text = [note valueForKey:@"title"];
  
      cell.detailTextLabel.text = dateString;
  
       cell.selectionStyle = UITableViewCellSelectionStyleNone;
  
       // cell.accessoryType = UITableViewCellAccessoryCheckmark;
  
      UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(5, 5, 40, 40)];
  
      [button addTarget:self action:@selector(checkBoxClicked:) forControlEvents:UIControlEventTouchUpInside];
  
      [button setImage:[UIImage imageNamed:@"uncheck.png"] forState:UIControlStateNormal];
  
      [cell addSubview:button];
  
      [cell setIndentationLevel:1];
  
      [cell setIndentationWidth:45];
  
      return cell;
  }
  
  - (IBAction)addButtonPressed:(id)sender {
  
      AddNoteViewController *addNoteVC = [AddNoteViewController new];
  }
  
  - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  {
    // Return NO if you do not want the specified item to be editable.
  
      return YES;
  }
  
  - (void)tableView:(UITableView *)cTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  {
      NSManagedObjectContext *context = [self managedObjectContext];
  
      if (editingStyle == UITableViewCellEditingStyleDelete) {
  
          // Delete object from database
  
          [context deleteObject:[self.notes objectAtIndex:indexPath.row]];
  
  
  
          NSError *error = nil;
  
          if (![context save:&amp;error]) {
  
              NSLog(@"Can't Delete! %@ %@", error, [error localizedDescription]);
             return;
          }
  
         // Remove device from table view
  
          [self.notes removeObjectAtIndex:indexPath.row];
  
          [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
  
      }
  }
  
  - (IBAction)btnClick:(id)sender {
  }
  
  -(UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
      return YES;
  }
  
  //#pragma Search Methods
  
  //
  
  //- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
  
  //{
  
  //    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", searchText];
  
  //    //    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"contains[c] %@", searchText];
  
  //    self.searchResults = [self.notes filteredArrayUsingPredicate:predicate];
  
  //}
  
  //
  
  //-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
  
  //{
  
  //    [self filterContentForSearchText:searchString
  
  //                               scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
  
  //                                      objectAtIndex:[self.searchDisplayController.searchBar
  
  //                                                     selectedScopeButtonIndex]]];
  
  //    
  
  //    return YES;
  
  //}
  
  @end
  </code></pre>