【问题标题】:SearchBar not displaying results until cancel button clickedSearchBar 在单击取消按钮之前不显示结果
【发布时间】:2010-10-28 10:49:10
【问题描述】:

我已经实现了一个带有搜索栏(和搜索显示)的 UITableView - 一切正常,但在点击搜索栏取消按钮之前表格结果不会更新。

委托方法:

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    // asynchronous request with [self.tableView reloadData] in the connectionDidFinishLoading
    [self getProductData:searchBar.text]; 
 [searchBar resignFirstResponder];
 [self.tableView reloadData];


}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {

}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {

}

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {

}

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar  {  
} 

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
 return YES;
}

从服务器收到数据后是否需要调用委托方法?还是应该让请求同步?

谢谢


编辑:我尝试了同步请求,但它仍然不起作用!

【问题讨论】:

    标签: iphone objective-c search uitableview uisearchbar


    【解决方案1】:

    通过将此代码添加到我的 getProductData 方法的末尾解决了这个问题:

    [[[self searchDisplayController] searchResultsTableView] performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
    

    【讨论】:

    • 太棒了,使用 [self performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];然后创建 -(void)reloadData{[table reloadData];} 它就像一个魅力:)
    【解决方案2】:

    我认为你必须实现:

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

    返回YES

    查看TableSearch 示例代码

    【讨论】:

    • 谢谢 - 我已经试过了,但还是不行。我还包括 - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
    • 嗯,我用的是 UISearchDisplayController,而不是 UISearchBarDelegate
    【解决方案3】:

    请尝试以下方法:

    - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
        [self getProductData:searchText]; 
        [self.tableView reloadData];
    }
    

    【讨论】:

    • 嗨,我已经尝试过了,这会导致每次搜索栏中的文本更改时都会调用 HTTP 请求 - 在我的情况下,这会导致应用程序崩溃。
    • 在这种情况下你应该考虑改变你的应用程序的设计。
    猜你喜欢
    • 2021-07-28
    • 1970-01-01
    • 2017-12-14
    • 1970-01-01
    • 2018-12-07
    • 1970-01-01
    • 1970-01-01
    • 2015-09-08
    • 1970-01-01
    相关资源
    最近更新 更多