UISearchController table 查看结果为空
UISearchController table view results empty
我一直在为这个问题苦苦挣扎。一些帖子处理了这个问题,但在我的案例中没有出现解决方案。
问题是:我想用 UISearchController 方法显示一些结果。我的带有自定义单元格的原始 table 视图在我的所有条目都在此处并按我的需要显示的意义上完美运行。
当我使用带有范围按钮的搜索栏时,再次一切正常,我过滤的结果正确地存储在列表中,甚至我的自定义单元格也得到了它需要的一切,从某种意义上说,当我检查单元格属性,它对应过滤条目...
但让我发疯的是显示的 "new" table 视图(带有结果的视图)是空的(从某种意义上说,所有视图都充满了空行)尽管过滤条目在这里并且 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)
}
我需要更多代码...
谢谢!
您的问题是您正在重新加载 self.tableView 而不是 searchController 的 tableView。
确保您还正确地将 searchController 的 tableView 的数据源和委托分配给了自己。
- (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
}
如果有帮助的话,这是更多代码
我一直在为这个问题苦苦挣扎。一些帖子处理了这个问题,但在我的案例中没有出现解决方案。
问题是:我想用 UISearchController 方法显示一些结果。我的带有自定义单元格的原始 table 视图在我的所有条目都在此处并按我的需要显示的意义上完美运行。
当我使用带有范围按钮的搜索栏时,再次一切正常,我过滤的结果正确地存储在列表中,甚至我的自定义单元格也得到了它需要的一切,从某种意义上说,当我检查单元格属性,它对应过滤条目...
但让我发疯的是显示的 "new" table 视图(带有结果的视图)是空的(从某种意义上说,所有视图都充满了空行)尽管过滤条目在这里并且 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)
}
我需要更多代码...
谢谢!
您的问题是您正在重新加载 self.tableView 而不是 searchController 的 tableView。
确保您还正确地将 searchController 的 tableView 的数据源和委托分配给了自己。
- (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
}
如果有帮助的话,这是更多代码