IOS搜索键盘慢

时间:2013-08-15 02:46:52

标签: ios keyboard uisearchbar

嗨我在我的应用程序中进行实时搜索,同时搜索并显示结果,这一切都很好但是键盘看起来有点滞后。

有没有办法让键盘不会滞后,仍然可以进行实时搜索。

这是搜索重新加载搜索结果的地方:

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{

[self filterListForSearchText:searchString scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];

return YES;

}

提前感谢。

2 个答案:

答案 0 :(得分:1)

当您搜索整个表字典时,您可以搜索尝试使用performSelectorInBackground:在单独的线程上搜索它。

[self performSelectorInBackground:filterListForSearchText withObject:searchString scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];

当您需要更新表格UI时,可以使用performSelectorOnMainThread:切换回主线程。

示例:[self performSelectorOnMainThread:updateTableMethod];

如果您不需要支持4.0以上的iOS版本,也可以尝试使用GCD

如果在后台搜索不起作用,表会尝试一次加载太多行。您可能希望仅在用户向下滚动时加载一定数量的行并加载更多行。

答案 1 :(得分:1)

感谢@AnsonL的选择,真的很有帮助。

如果有人想知道的话,这就是我最终的结果:

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{

dispatch_queue_t queue = dispatch_queue_create("com.yourdomain.yourappname", NULL);
dispatch_async(queue, ^{
    //code to be executed in the background

    [self filterListForSearchText:searchString scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];

    dispatch_async(dispatch_get_main_queue(), ^{
        //code to be executed on the main thread when background task is finished
        [searchDisplayController.searchResultsTableView reloadData];

    });
});

return NO;

}