iOS UISearchDisplayController 不显示结果

标签 ios cocoa-touch uitableview uisearchbar uisearchdisplaycontroller

我对 UISearchDisplayController 有疑问。我使用 Storyboard 添加了一个 UITableView 到 UIViewControllerUITableViewDataSourceUITableViewDelegate 连接到 Storyboard 中的 myViewController。

我在 myViewController 中实现了以下 delegatesdatasources:

UISearchBarDelegateUISearchDisplayDelegateUITableViewDataSourceUITableViewDelegate。 在表格 View 中显示所有数据工作正常,但是当我开始搜索时,调用了 numberOfRowsInSection: 方法,但之后 cellForRowAtIndexPath: 没有被调用,并且 searchResultsTableView 根本不显示。 有人可以帮助我吗?

下面是UISearchDisplayDelegateUISearchBarDelegate方法的代码。

编辑:

static NSString *ProductCellIdentifier = @"TKMProductTableCell";
@interface TKMSearchAllProductsTableVC () <UISearchBarDelegate, UISearchDisplayDelegate, UITableViewDataSource, UITableViewDelegate> {
    UISearchDisplayController *searchDisplayController;
}

@property (nonatomic, strong) NSArray *allProducts;
@property (nonatomic, strong) NSMutableArray *filteredProducts;
@property (nonatomic, strong) UISearchBar *searchBar;

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == searchDisplayController.searchResultsTableView) {
        return [_filteredProducts count];
    } else {
        return [_allProducts count];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    TKMProductTableCell *cell = [self.tableView dequeueReusableCellWithIdentifier:ProductCellIdentifier];

    // Configure the cell...
    [self configureProductCell:cell atIndexPath:indexPath inTableView:tableView];

    return cell;
}

- (void)configureProductCell:(TKMProductTableCell *)cell atIndexPath:(NSIndexPath *)indexPath inTableView:(UITableView *)tableView
{
    TKMProduct *product = nil;
    if (tableView == searchDisplayController.searchResultsTableView) {
        product = self.filteredProducts[indexPath.row];
    } else {
        product = self.allProducts[indexPath.row];
    }

    cell.lblTitle.text = product.name;

}
#pragma mark - UISearchBarDelegate
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    [self.navigationItem setRightBarButtonItem:nil];
    searchBar.showsCancelButton = YES;

    searchBar.autocorrectionType = UITextAutocorrectionTypeNo;

    [searchDisplayController setActive:YES animated:YES];
}

-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
    searchBar.showsCancelButton = NO;
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    searchBar.showsCancelButton = NO;

    [searchBar resignFirstResponder];
    [searchDisplayController setActive:NO animated:YES];
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - UISearchDisplayDelegate

- (void)filterContentForSearchText:(NSString *)searchText
{
    [self.filteredProducts removeAllObjects];

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name contains[c] %@", searchText];
    _filteredProducts = [NSMutableArray arrayWithArray:[_allProducts filteredArrayUsingPredicate:predicate]];
}

- (void)searchDisplayController:(UISearchDisplayController *)controller didShowSearchResultsTableView:(UITableView *)tableView
{
    tableView.backgroundColor = [UIColor redColor];
}

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
    {
    if (![searchString isEqualToString:@""] && searchString != nil) {
        [self filterContentForSearchText:searchString];
        [searchDisplayController.searchResultsTableView setHidden:NO];
    }

    return YES;
}

编辑 2

这是我的 UISearchDisplayController 代码:

searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
searchDisplayController.delegate = self;
searchDisplayController.searchResultsDataSource = self;
searchDisplayController.searchResultsDelegate = self;

这是 Storyboard连接的屏幕截图:
screenshoot of storyboard connections

编辑 3:

我刚刚在日志中发现 UITableView 的 框架是numberOfRowsInSection 方法中的CGRectZero:

(lldb) po [searchDisplayController searchResultsTableView]
<UISearchResultsTableView: 0x1368c4a00; frame = (0 0; 0 0); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x1744420d0>; layer = <CALayer: 0x174821e20>; contentOffset: {0, 0}; contentSize: {0, 0}>

(lldb) po [self.searchDisplayController searchResultsTableView]
<UISearchResultsTableView: 0x1368c4a00; frame = (0 0; 0 0); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x1744420d0>; layer = <CALayer: 0x174821e20>; contentOffset: {0, 0}; contentSize: {0, 0}>

(lldb) po self.tableView
<UITableView: 0x136878e00; frame = (0 0; 320 504); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x17444dd70>; layer = <CALayer: 0x17422e300>; contentOffset: {0, 0}; contentSize: {320, 117906}>

(lldb) po tableView
<UISearchResultsTableView: 0x1368c4a00; frame = (0 0; 0 0); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x1744420d0>; layer = <CALayer: 0x174821e20>; contentOffset: {0, 0}; contentSize: {0, 0}>

最佳答案

建议

我可能有点跑题了,但你应该知道这一点。

不要使用 UISearchDisplayController 因为它不再稳定。

Apple 文档

Important: UISearchDisplayController is deprecated in iOS 8. (Note that UISearchDisplayDelegate is also deprecated.) To manage the presentation of a search bar and display search results in iOS 8 and later, instead use UISearchController.

Link

关于iOS UISearchDisplayController 不显示结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28190763/

相关文章:

ios - objective c renderInContext 在后台线程上崩溃

ios - Ios UITableview 中的自适应表格 View 单元格

iphone - 添加或删除单元格后如何重新加载所有表格单元格?

ios - 将 NSUrl 转换为 PHAsset iOS 8

类似于苹果商店的ios过滤选项(下拉列表)

ios - 在 UITests 中解决 "Main Thread Checker: UI API called on a background thread: -[UIApplication setNetworkActivityIndicatorVisible:]"

ios - 在应用程序中存储 iPhone 应用程序设置

html - 为什么我的网站在 iPad 上只显示一张图片?

ios - 核心基础vs基础或核心基础+基础

ios - 当 View Controller 已经从基础 View Controller 继承时,如何在 View Controller 上创建 TableView