所以我不确定我是否正确设置了它。我有一个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
,但这会给我相同的结果。
所以记录得到的更具体的例子是:
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)
有什么想法?提前谢谢!
答案 0 :(得分:0)
好的我明白了。 reloadData确实有效。问题是我试图打电话
[self.searchDisplayController.searchResultsTableView reloadData];
而不是
[self.SearchController.searchResultsTableView reloadData];