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また、[result count] に含まれる項目の数を確認するためのログもメソッドに記録しています。私が見ると、基本的に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 に答える