TableView数据没有得到更新与NSOperation

问题描述:

所以我不知道如果我正确地设置它。我有一个SearchDisplayController和搜索栏。TableView数据没有得到更新与NSOperation

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子类中。最后,它通过委托回调的视图控制器来更新当前数据:当实际输入进去我的搜索栏

[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) 

有什么想法?提前致谢!

好吧我想通了。 reloadData确实有效。问题是,我试图调用

[self.searchDisplayController.searchResultsTableView reloadData]; 

代替

[self.SearchController.searchResultsTableView reloadData];