简体   繁体   中英

didSelectRowAtIndexPath does not work when uitableview datasource is filtered

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

经过一番搜索,我发现我应该再设置一件事了

searchDisplayController.searchResultsDelegate = self;

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