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 condition in the 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
reuseIdentifier:CellIdentifier];
cell.nameofani.text=animal.nameOfAnimal;
cell.oriofani.text=animal.originOfAnimal;
}
return cell;
}
try: