UISearchController表视图结果为空

时间:2016-11-09 17:15:21

标签: ios uitableview uisearchcontroller is-empty

我一直在努力解决这个问题。有些帖子处理了这个问题,但在我的案例中没有出现任何解决方案。

问题出在这里:我想用UISearchController方法显示一些结果。我的原始表视图与自定义单元格完美地工作,因为我的所有条目都在这里,并显示我想要的。

当我使用带有范围按钮的搜索栏时,所有内容都完美运行,我的过滤结果正确存储在列表中,甚至我的自定义单元格也可以获得所需的一切,就在我检查单元属性时,它对应于过滤的条目...

但是我疯狂的地方是显示的“新”表格视图(带有结果的视图)是空的(在所有视图都用空行填充的意义上),尽管过滤的条目在这里并且cellForRowAtIndexPath正在完成它的工作(至少在它必须显示之前)。

有人可以帮我解决这个问题吗?

代码:

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {

    NSPredicate *predicate;

    NSString *searchText = searchController.searchBar.text;
    NSInteger scope = searchController.searchBar.selectedScopeButtonIndex;

    if (scope == 0) {
        predicate = [NSPredicate predicateWithFormat:@"SELF.drugName contains[c] %@", searchText];

    }

    if (scope == 1) {
        predicate = [NSPredicate predicateWithFormat:@"SELF.marque contains[c] %@", searchText];

    }

    else if (scope == 2) {
        predicate = [NSPredicate predicateWithFormat:@"SELF.classFamily contains[c] %@", searchText];

    }

    NSMutableArray *tempArray = [NSMutableArray arrayWithArray:[[[SubstrateStore sharedStore] allSubstrates] filteredArrayUsingPredicate:predicate]];
    self.filteredResults = [NSMutableArray arrayWithArray:tempArray];

    [self.tableView reloadData]; // -> I tried to remove, to change the table view, etc, it does change anything
}

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

    TableViewCell *cell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:kCellIdentifier forIndexPath:indexPath];

    Substrate *substrate = self.products[indexPath.row];

    if (_searchController.active && ![_searchController.searchBar.text isEqualToString:@""]) {

        substrate = self.filteredResults[indexPath.row];
    }

    [self configureCell:cell forProduct:substrate]; // when I check cell attributes (drugName,...), they are there...

    return cell; // it works when no search is done (with the native table view) but the cell is not displayed when using the search bar (empty table view)
}

我需要更多代码......

谢谢!

2 个答案:

答案 0 :(得分:0)

您的问题是您正在重新加载self.tableView而不是searchController的tableView。

确保您也正确地将searchController的tableView的dataSource和delegate分配给self。

答案 1 :(得分:0)

- (void)viewDidLoad {
    [super viewDidLoad];

    self.products = [[SubstrateStore sharedStore] allSubstrates];
    self.filteredResults = [[NSArray alloc] init];

    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

    // SearchBar
    _resultsTableController = [[ResultsTableViewController alloc] init];
    _searchController = [[UISearchController alloc] initWithSearchResultsController:self.resultsTableController];
    self.searchController.searchResultsUpdater = self;
    [self.searchController.searchBar sizeToFit];
    self.tableView.tableHeaderView = self.searchController.searchBar;
    self.searchController.searchBar.showsScopeBar = YES;
    self.searchController.searchBar.scopeButtonTitles = [NSArray arrayWithObjects:@"DCI", @"Nom de marque", @"Classe", nil];

    // we want to be the delegate for our filtered table so didSelectRowAtIndexPath is called for both tables
    self.resultsTableController.tableView.delegate = self;
    self.searchController.delegate = self;
    self.searchController.dimsBackgroundDuringPresentation = NO; // default is YES
    self.searchController.searchBar.delegate = self; // so we can monitor text changes + others

    // Search is now just presenting a view controller. As such, normal view controller
    // presentation semantics apply. Namely that presentation will walk up the view controller
    // hierarchy until it finds the root view controller or one that defines a presentation context.
    //
    self.definesPresentationContext = YES;  // know where you want UISearchController to be displayed

}

如果能提供帮助,可以使用更多代码