使用UISearchBarsearch表格视图

我正在使用这个代码来search一个UItableView:

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { if(searchText.length == 0) { isFiltered = FALSE; } else { isFiltered = TRUE; if (filteredTableData == nil) filteredTableData = [[NSMutableArray alloc] init]; else [filteredTableData removeAllObjects]; for (NSString* string in self.itemArray) { NSRange nameRange = [string rangeOfString:searchBar.text options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)]; if(nameRange.location != NSNotFound) { [filteredTableData addObject:string]; } } } [tableView reloadData]; [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade]; } 

一切正常,但如果我按开始编辑时出现的取消button,我的列表不会回来,但search结果仍然存在。 为了显示列表,我必须开始input,甚至在search栏中input一个字符,然后删除它或按“x”查看所有列表。 有没有办法阻止取消button? 或者当它被按下时显示列表?

实现 – (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar UISearchBar的委托方法。 在那里添加filteredTableData数组中的所有对象(表中的初始对象)并重新加载表。

  -(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar { [filteredTableData removeAllObjects]; [filteredTableData addObjectsFromArray:self.itemArray]; [tableView reloadData]; } 

如果您正在使用它来select用于重新载入表的数据源数组(在表视图数据源方法中),则不需要维护标志isFiltered。 例如

 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if(isFiltered) return filteredTableData.count else return self.itemArray.count } 

如果您始终在filteredTableData数组中维护数据,则不需要执行此操作。 你的方法看起来像 –

 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return filteredTableData.count } 

最初在控制器的init或viewDidLoad方法中,添加filteredTableData中的所有对象,并仅使用此数组重新加载表。

因此,你的方法看起来像 –

  -(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { if(searchText.length == 0) { [filteredTableData removeAllObjects]; [filteredTableData addObjectsFromArray:self.itemArray]; } else { [filteredTableData removeAllObjects]; for (NSString* string in self.itemArray) { NSRange nameRange = [string rangeOfString:searchBar.text options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)]; if(nameRange.location != NSNotFound) { [filteredTableData addObject:string]; } } } [tableView reloadData]; } 

我不使用这种方法,但使用以下,它完美的作品

 - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString { NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name CONTAINS[cd] %@",searchString]; self.filteredCustomers = [[self.customers filteredArrayUsingPredicate:predicate] mutableCopy]; return YES; } - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar { [self.searchDisplayController setActive:NO animated:NO]; self.filteredCustomers = nil; [self.tableView reloadData]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"CustomerCell"; NSDictionary *customerObject; CustomerListCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (self.searchDisplayController.active) { customerObject = [[NSDictionary alloc] initWithDictionary:[self.filteredCustomers objectAtIndex:indexPath.row]]; } else { customerObject = [[NSDictionary alloc] initWithDictionary:[[self.customerData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]]; } cell.textLabel.text = [customerObject objectForKey:@"name"]; cell.customerName = [customerObject objectForKey:@"name"]; cell.customerId = [customerObject objectForKey:@"customer_id"]; return cell; }