2

UISearchDisplayController通过NSOperationQueue.

然而,结果表直到NSOperation调用后大约 5 秒才直观地更新[searchDisplayController.searchResultsTableView reloadData]

- (BOOL) searchDisplayController:(UISearchDisplayController*)controller shouldReloadTableForSearchString:(NSString*)searchString
{
    [searchQueue cancelAllOperations];
    NSInvocationOperation *op = [[[CustomSearchOperation alloc] initWithController:controller searchTerm:searchString] autorelease];
    [searchQueue addOperation:op];

    return NO;
}

我的 CustomerSearchOperation 像这样更新 tableView:

- (void) main
{
    // perform search

    [searchDisplayController setContents:results];
    [searchDisplayController.searchResultsTableView reloadData];
}
4

1 回答 1

4

问题是 UI 更新必须发生在主线程上,并且reloadData是通过NSOperationQueue.

您可以使用NSObject方法performSelectorOnMainThread:withObject:waitUntilDone:来确保此类更新发生在主线程上。

- (void) main
{
    // perform search

    [sdc setContents:results];
    [sdc.searchResultsTableView performSelectorOnMainThread:@selector(reloadData)
                                withObject:nil
                                waitUntilDone:NO];
}
于 2011-02-09T14:49:11.630 回答