2013-02-12 37 views
0

所以我不知道如果我正確地設置它。我有一個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) 

有什麼想法?提前致謝!

回答

0

好吧我想通了。 reloadData確實有效。問題是,我試圖調用

[self.searchDisplayController.searchResultsTableView reloadData]; 

代替

[self.SearchController.searchResultsTableView reloadData];