Buscando usando UISearchBar

I have a search bar added to the navigation bar. When the user types a word and clicks the search button (keyboard) I need to display results in the table related to the search.

When I click "Search", I get blank records (it actually goes inside the if condición en el searchBarSearchButtonClicked: method and adds the animal object too). I think there's a problem in

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

My complete code is as follows.

-(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 
    return cell;

preguntado el 31 de enero de 12 a las 16:01

2 Respuestas

Asegúrate de implementar tableView:numberOfRowsInSection: correctly, returning [self.animalArray count] for the search results table view.

Respondido el 31 de enero de 12 a las 23:01


self.animalArray = [mutableArray copy];

Respondido el 31 de enero de 12 a las 21:01

Still i get blank records. When i print the count of self.animalArray it gives the result as 1. So there is a match found and for some reason its not getting shown in the table. Help ? - Illep

Did you try NSLog(@" self.animalArray = %@", self.animalArray); to see what the match is? - Ader

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.