我一直在爲這個問題苦苦掙扎。有些帖子處理這個問題,但在我的情況下沒有解決方案。UISearchController表視圖結果爲空
這是問題:我想用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)
}
我需要更多的代碼...
謝謝!
1.你肯定'tempArray'有任何對象? 2.你確定'self.filteredResults'是'self.tableView'的正確dataSource嗎? 3.當你調用'[self.tableView reloadData]',任何'UITableViewDelegate'方法被調用? – Ryan
問題是我忘記將數據源設置爲自己的結果tableview – Trichophyton