我有一个带有searchDisplayController的uitableview和一个搜索栏,我使用来自数据库的数据填充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];
}
非常感谢inadvance
答案 0 :(得分:17)
searchDisplayController.searchResultsDelegate = self;