Search code examples
objective-cios9reloaddatauisearchcontroller

use of UISearchController but tableView is not reloaded


I am trying to add search bar to search through my tableView. There is only one TableViewController in which searchBar is added. I checked all existing guides and apple example codes and followed the instructions but somehow my tableView is not reloaded after entering search string. I checked my codes with NSLog, and I'm sure the the search text is correctly uploaded and and the new array is filtered using NSPredicate.I have added [self.tableView reloadData] at the end of "updateSearchResultsForSearchController:" method but still my tableViewController is not updated. I am new to iOS programming and not sure what i am doing wrong. Appreciate if you can have a quick glance on my codes and feedback to me. Here is the summary :

@property (nonatomic,strong) UISearchController *searchController;
@property (nonatomic,strong) NSArray *myList;   //original data list
@property (nonatomic,strong) NSArray *mysearchResult;   //filtered list



  - (void)viewDidLoad {
        [super viewDidLoad];
        self.searchController=[[UISearchController alloc]initWithSearchResultsController:nil];
        self.searchController.searchResultsUpdater=self;
        self.searchController.dimsBackgroundDuringPresentation=NO;
        self.searchController.searchBar.delegate=self;
        self.tableView.tableHeaderView=self.searchController.searchBar;
        self.definesPresentationContext=YES;
        [self.searchController.searchBar sizeToFit];
    }
    -(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
    NSString *searchString=self.searchController.searchBar.text;
    NSPredicate *mypredicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@", searchString];
    self.mysearchResult=[self.myList filteredArrayUsingPredicate:mypredicate];
    [self.tableView reloadData];
}

"CellForRowAtIndexPath" method for tableView codes:(similarly "numberOfRowsInSection" is updated)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MyCustomCell codes..... 
if (tableView==self.tableView) {
        cell.cellLabel.text=[self.myList objectAtIndex:indexPath.row];
    } else 
        cell.cellLabel.text=[self.mysearchResult objectAtIndex:indexPath.row];

    return cell;
}

Solution

  • For first time before populating the tableView use mysearchResultinstead of myList. At this point data in both arrays should be same.

    self.mysearchResult = [[NSArray alloc] initWithArray:myList];
    

    Now after using predicate you are updating your mysearchResult that's fine. While you cancel search again populate your mysearchResult from myList which actually contains all your original data.

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        [self.mysearchResult count];
    }
     - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyCustomCell codes..... 
    
    //Remove your If condition from here
            cell.cellLabel.text=[self.mysearchResult objectAtIndex:indexPath.row];
        return cell;
    }