2012-01-31 84 views
0

我有一個搜索欄添加到導航欄。當用戶鍵入一個單詞並單擊搜索按鈕(鍵盤)時,我需要在與搜索相關的表中顯示結果。使用UISearchBar搜索

當我點擊「搜索」時,我得到空白記錄(它實際上在searchBarSearchButtonClicked:方法中的if條件內並且也添加動物對象)。我認爲有問題

[mutableArray addObject:animal]; 
... 
self.animalArray = mutableArray;  
[self.tableView reloadData]; 

我的完整代碼如下。

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{ 
    [self.mutableArray removeAllObjects]; 
    for (Animal *animal in self.animalArray) { 
     if ([[animal nameOfAnimal] isEqualToString:[searchBar text]]) {     
      [mutableArray addObject:animal]; 
     } 
    } 
    self.animalArray = mutableArray;  
    [self.tableView reloadData]; 
    [searchBar resignFirstResponder]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    Cell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) {   
     Animal *animal = [self.animalArray objectAtIndex:indexPath.row];   
     cell = [[Cell alloc] initWithStyle:UITableViewCellStyleDefault 
          reuseIdentifier:CellIdentifier]; 
     cell.nameofani.text=animal.nameOfAnimal; 
     cell.oriofani.text=animal.originOfAnimal; 
    } 
    return cell; 
} 

回答

1

嘗試:

self.animalArray = [mutableArray copy]; 
+0

我還記得空白。當我打印self.animalArray的計數時,它給出的結果爲1.所以找到了一個匹配,並且由於某種原因它沒有顯示在表中。幫幫我 ? – Illep 2012-01-31 17:38:35

+0

你試過NSLog(@「self.animalArray =%@」,self.animalArray);看看比賽是什麼? – ader 2012-02-01 09:26:52

2

確保您實現tableView:numberOfRowsInSection:正確的,對於搜索結果表視圖返回[self.animalArray count]

相關問題