2014-02-05 58 views
0

我已經創建了一個搜索方法,它可以搜索tableview的內容。這工作正常,每次我在搜索欄中寫入時,它都會將該對象添加到名爲filteredArray的數組中,並顯示等於filteredArray.count的tableviewcell的數量。我用NSLog測試了這個(@「%@」,filteredArray);搜索欄結果不會顯示在桌面視圖中

問題是我想在你搜索時在tableview中顯示filteredArray。我已經改變了這個cellForRowAtIndexPath方法,但它只是給我空的tableviewcells。我究竟做錯了什麼?

不搜索:

enter image description here

搜索 「RU」:

enter image description here

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    LanguageCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (!cell) { 
     cell = [[LanguageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    } 

     if (tableView == self.tableViewData) { 
    cell.patternLabel.text = [NSString stringWithFormat:@"%@", [[rows objectAtIndex:indexPath.row]objectAtIndex:0]]; 


     } else{ 
      cell.patternLabel.text = [NSString stringWithFormat:@"%@", [filteredArray objectAtIndex:indexPath.row]]; 

     } 
    return cell; 
} 

搜索方法:

-(void) searchThroughdata { 

    self.filteredArray = nil; 
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains [c] %@",self.searchBar.text]; 


    self.filteredArray = [[finalArray filteredArrayUsingPredicate:predicate] mutableCopy]; 

    NSLog(@"%@", filteredArray); 



} 

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { 
    [self searchThroughdata]; 
} 
+0

顯示您的搜索委託方法代碼。 – Wain

+0

完成。我已經添加了方法和2張圖片,說明了當我不搜索時以及當我搜索時。 – user3258468

+0

'LanguageCell' - 是否在表格中註冊了單元格?什麼時候'cell = [[LanguageCell alloc] initWithStyle:...'正在運行? – Wain

回答

0

您有兩個表格視圖,單元格是註冊一個而不是另一個 - 這是原型單元格在您將其定義在故事板中時工作的方式我想你已經完成了。

當搜索表視圖沒有獲取出隊單元格時,您可以使用initWithStyle:reuseIdentifier:創建一個單元格,但這不會創建patternLabel。所以,你不會崩潰,因爲你返回一個單元格,但是單元格是空的,因爲標籤不存在。

你最好的選擇:

  1. 取出原型細胞,創建一個XIB並與每個表視圖註冊NIB明確
  2. 不使用搜索控制器(所以你只有1個表視圖,管理您自己的搜索欄)
1

添加新字符後,您可以使[yourTableView reloadData]