我正在尝试设置一个搜索显示控制器来处理来自网络服务的异步结果。我已经掌握了基本知识,但遇到了一个我无法弄清楚的非常奇怪的问题。
似乎要为异步设置搜索显示控制器,您实际上只需要做两件事:
- 为 searchDisplayController:shouldReloadTableForSearchString 返回 NO,并且
- 处理 searchBarSearchButtonClicked 并关闭表重新加载自己。
我正在做这两件事,但我看到的是搜索显示控制器正在重新加载输入到搜索栏中的第一个字符的表格,即使我按照 #1 返回 NO。它不会在输入的后续字符上重新加载。
所以,我的问题是:如何防止搜索显示控制器在用户键入时尝试重新加载表格?(特别是在输入的第一个字符上)
我已经看到这个问题是作为其他几个问题的一部分提到的,但我还没有看到这个问题的直接答案。在我求助于一堆 UI 修改来解决它之前,我想了解发生了什么或我做错了什么。
这是我的代码的快速提炼以显示问题。当我运行它并在搜索栏中输入“abcde”时,在我输入“a”后,结果显示为“a #0”、“a #2”等。它们不会再次更新,直到我点击搜索按钮然后你会看到“abcde #0”、“abcde #1”等。当然,在我点击搜索按钮之前,什么都不会发生。
#pragma mark -
#pragma mark UISearchDisplayController Delegate Methods
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
return NO;
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption {
return NO;
}
#pragma mark -
#pragma mark UISearchBarDelegate Methods
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
[self.searchDisplayController.searchResultsTableView reloadData];
}
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [self.searchDisplayController.searchBar.text stringByAppendingFormat:@" #%d", indexPath.row];
return cell;
}
谢谢!(顺便说一句,这是我在这里提出的第一个问题——如果我错过任何礼仪要点,请告诉我:)