我有 uitableview 和一个带有 searchDisplayController 的搜索栏,我用数据库中的数据填充 tableview 数据源,我使用用户搜索栏进行搜索,一次未过滤 uitableview 数据源,didSelectRowAtIndexPath 工作正常,它转到 detailviewcontroller,但是当我使用搜索时并过滤数据源,然后如果我选择一个单元格,didSelectRowAtIndexPath 方法不会触发。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
KardexViewController* kardexViewController = [[KardexViewController alloc] initWithStyle:UITableViewStylePlain];
[self.navigationController pushViewController:kardexViewController animated:YES];
}
这就是我添加 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;
}
以及我如何过滤数据源
- (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];
}
所以谢谢提前