简体   繁体   中英

display search result in table view in objective-c

  1. I've created a table view with search bar in my View Controller, but the table view doesn't show the search result.
  2. This is how my code working. First i create an Array to hold data >> display the array data in table view >> using search bar to filter the table view.

Here is my code :

- (void)viewDidLoad {
    [super viewDidLoad];
    self.inventoryarray =[[NSArray alloc] initWithObjects: @"apple",@"samsung",@"HTC",@"LG",@"Sony",@"Motorola",@"Nexus",@"Asus" ,nil];
    self.searchresult =[[NSArray alloc]init];    
    // Do any additional setup after loading the view, typically from a nib.
}

#pragma table View methods

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return [self.searchresult count];
    } else {
        return [self.inventoryarray count];
    }
}    

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {     
    static NSString *simpleTableIdentifier = @"CellID";    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        cell.textLabel.text = [self.searchresult objectAtIndex:indexPath.row];
    } else {
        cell.textLabel.text = [self.inventoryarray objectAtIndex:indexPath.row];
    }    
    return cell;    
}

#pragma search methods

-(void) filterContentForSearchText:(NSString *)searchText scope:(NSString *)scope
{
    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@", searchText];    
    self.searchresult = [self.inventoryarray filteredArrayUsingPredicate:resultPredicate];
}


-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString  {    
    [self filterContentForSearchText:searchString scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]] ;
    return YES;
}

you forget to refesh data on table

 -(void) filterContentForSearchText:(NSString *)searchText scope:   (NSString *)scope
{
    NSPredicate *resultPredicate = [NSPredicate
                            predicateWithFormat:@"SELF contains[cd] %@",
                            searchText];

    self.searchresult = [self.inventoryarray filteredArrayUsingPredicate:resultPredicate];

    // you forget to reload Data
    [self.searchDisplayController.searchResultsTableView reloadData];
    // else use 

    [yourtableView reloadData];
}

suggestion : UISearchDisplayController is deprecated, use UISearchController on onwards, for tutorial

You need to only [tableView reloadData]; in last of filterContentForSearchText method.

if you wan't to use UISearchController then... - 'UISearchDisplayController' is deprecated: first deprecated in iOS 8.0 - UISearchDisplayController has been replaced with UISearchController

if you don't want to use UISearchController then use below method:

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {} and reload tableview.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM