2012-01-31 89 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]

相关问题