2016-11-09 24 views
1

我一直在爲這個問題苦苦掙扎。有些帖子處理這個問題,但在我的情況下沒有解決方案。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) 
} 

我需要更多的代碼...

謝謝!

+0

1.你肯定'tempArray'有任何對象? 2.你確定'self.filteredResults'是'self.tableView'的正確dataSource嗎? 3.當你調用'[self.tableView reloadData]',任何'UITableViewDelegate'方法被調用? – Ryan

+0

問題是我忘記將數據源設置爲自己的結果tableview – Trichophyton

回答

0

你的問題是你重新加載self.tableView而不是searchController的tableView。

確保您還正確地將searchController的tableView的dataSource和委託分配給self。

+0

這是我以前讀過的東西,但即使嘗試重新加載篩選的表視圖它似乎不工作。或者我可能會錯誤地做到這一點。我應該在同一地點重新加載數據嗎? – Trichophyton

+0

因爲我讀過使用同一個tableview來顯示這個方法的結果。怎麼可能重新加載searchController的表格視圖?我不確定我是否創建了另一個tableview – Trichophyton

+0

您有兩張表,聽起來像。你的VC有一個主表,它正在更新,因爲你調用'[self.tableView reload];',並且你有searchView中存在的tableView。我認爲它可能在searchController.searchResults或類似的。在該方法中設置斷點並檢查內存堆棧以查找確切值。您的searchController對正在顯示的空白表格有引用,並且您從不更新該表格的數據,因此它保持空白。確保將委託/數據源分配給自己以獲得結果表格視圖。 –

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 

} 

這是一些更多的代碼,如果它可以幫助