0

所以我不确定我是否正确设置了它。我有一个SearchDisplayController搜索栏。

UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 150, 44)];
    self.SearchEntry = searchBar;
    self.SearchEntry.tintColor = DARKGRAY_COLOR;
    self.SearchEntry.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
    self.SearchEntry.delegate = self;
    UISearchDisplayController *searchDisplayCtlr = [[UISearchDisplayController alloc] initWithSearchBar:_searchEntry contentsController:self];
    self.SearchController = searchDisplayCtlr;

这些进入一个UIToolbar. 因为在数据库中查询某些值可能需要一段时间,所以我将代码取出并放在一个NSOperation子类中。最后,它通过委托回调 ViewController 以更新实际数据:

[self.searchOperationDelegate searchDidFinishWithResults:self.searchResults];

因此,当在我的搜索栏中进行实际输入时,我会这样做:

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    if (_queue.operationCount > 0) {
        [_queue cancelAllOperations];
    }

    SearchOperation *search = [[SearchOperation alloc] initWithSearchText:searchText];
    search.searchOperationDelegate = self;
    [self.queue addOperation:search];
}

我基本上取消了任何以前的搜索,只搜索searchText字符串中的当前内容。

然后在我的 ViewController 中的委托回调方法中

- (void)searchDidFinishWithResults:(NSArray *)results {
        NSLog(@"resultsList: %i", [results count]);
    [self performSelectorOnMainThread:@selector(setResultsList:) withObject:results waitUntilDone:YES];
//    [self.searchDisplayController.searchResultsTableView reloadData];
}

我还有一个登录cellForRowAtIndexPath方法来检查我的 [结果计数] 中有多少项目。当我查看时,我基本上被cellForRowAtIndexPath调用了很多次(根据我们正在搜索的内容说 1000-3000),但是在我的searchDidFinishWithResults:方法中,我得到了 1 个项目。它总是cellForRowAtIndexPath被调用,然后是这个searchDidFinishWithResults:方法。所以在我更新模型后,表格不再更新,我不知道为什么。我以为我可以手动调用reloadData,但这给了我相同的结果。

所以 get 记录的一个更具体的例子是:

resultsList: 2909 (I type in one key, and only this gets logged)
tableView:cellForRowAtIndexPath:] 2909 (my second key in the search bar, this gets logged)
tableView:cellForRowAtIndexPath:] 2909
tableView:cellForRowAtIndexPath:] 2909
tableView:cellForRowAtIndexPath:] 2909
tableView:cellForRowAtIndexPath:] 2909
tableView:cellForRowAtIndexPath:] 2909
tableView:cellForRowAtIndexPath:] 2909
resultsList: 1370 (this is the last thing that gets logged when my 2nd key is pressed)
tableView:cellForRowAtIndexPath:] 1370 (this is the first thing that gets logged when my 3rd key is pressed)
tableView:cellForRowAtIndexPath:] 1370
tableView:cellForRowAtIndexPath:] 1370
tableView:cellForRowAtIndexPath:] 1370
tableView:cellForRowAtIndexPath:] 1370
tableView:cellForRowAtIndexPath:] 1370
tableView:cellForRowAtIndexPath:] 1370
resultsList: 1 (last thing that gets logged when my 3rd key is pressed)

有什么想法吗?提前致谢!

4

1 回答 1

0

好的,我想通了。reloadData 确实有效。问题是我试图打电话

[self.searchDisplayController.searchResultsTableView reloadData];

代替

[self.SearchController.searchResultsTableView reloadData];
于 2013-02-12T22:25:38.163 回答