2
votes

i have uitableview and a search bar with searchDisplayController, i fill tableview datasource with data from database and i user search bar for searching, at a time uitableview datasource is not filtered the didSelectRowAtIndexPath works fine and it goes to the detailviewcontroller, but when i use search and filter datasource then if i select a cell the didSelectRowAtIndexPath method does not fire.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    KardexViewController* kardexViewController = [[KardexViewController alloc] initWithStyle:UITableViewStylePlain];

    [self.navigationController pushViewController:kardexViewController animated:YES];
}

this is how i add uisearchbar

- (void)viewDidLoad
{
    [super viewDidLoad];

    dataSource = [[NSMutableArray alloc] init];

    self.navigationItem.title = Title;

    searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];

    searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
    searchDisplayController.delegate = self;
    searchDisplayController.searchResultsDataSource = self;

    self.tableView.tableHeaderView = searchBar; 
}

and how i filter datasource

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    NSMutableArray* result = [[NSMutableArray alloc] init];

    for (int i = 0; i < [self.allData count]; i++) {
        NSRange range = [[[self.allData objectAtIndex:i] valueForKey:@"Code"] rangeOfString:searchString];

        if(range.length > 0)
            [result addObject:[self.allData objectAtIndex:i]];

        range = [[[self.allData objectAtIndex:i] valueForKey:@"Name"] rangeOfString:searchString];

        if(range.length > 0)
            [result addObject:[self.allData objectAtIndex:i]];
    }

    self.dataSource  = [result copy];
    [result release];
    [self.searchDisplayController.searchResultsTableView reloadData];

    return YES;
}

-(void) searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller
{
    self.dataSource  = [self.allData copy];
}

So Thanks inadvance

1
please provide code for how you use search and filer the table in it there will some problem and so it does not work properly or at allThe iOSDev

1 Answers

17
votes

after some search i found that i just should set one thing more

searchDisplayController.searchResultsDelegate = self;