User1075
User1075

Reputation: 885

How to adjust the image according to searched item in ios?

I ma using a UISearch bar to filter the lists in table, Every thing works fine but the problem in correct image is not shown according to text...Please see below images. Any help will be appriciated... Initial image before search

After Search

  -(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {



    if(searchText.length == 0)
    {
        _isFiltered = FALSE;
    }
    else{
        // NSPredicate *resultPredicate=[NSPredicate predicateWithFormat:@"(SELF.name contains[cd] %@)or(SELF.rank contains[cd] %@)or(SELF.score contains[cd] %@)",searchText,searchText,searchText];
        NSPredicate *resultPredicate=[NSPredicate predicateWithFormat:@"(SELF contains[cd] %@)",searchText];
        self.filteredTableData = [self.searchItemsArray filteredArrayUsingPredicate:resultPredicate];
        NSLog(@"Predicated array %@", self.filteredTableData);
        self.isFiltered = YES;
        if (self.filteredTableData.count == 0) {
            [[CLAlertHandler standardAlertHandler]showAlert:@"No match found!" title:AppName];
        }
    }
    //[self.searchTableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
    [self.searchTableView reloadData];
}


    (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SearchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if(cell == nil)
        cell = [[SearchTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    if (_isFiltered) {
        cell.itemNameLabel.text = [self.filteredTableData objectAtIndex:indexPath.row];
        cell.iconImageView.image = [UIImage imageNamed:[self.searchImagesArray objectAtIndex:indexPath.row]];
    }
    else
    {
    cell.itemNameLabel.text = [self.searchItemsArray objectAtIndex:indexPath.row];
    cell.iconImageView.image = [UIImage imageNamed:[self.searchImagesArray objectAtIndex:indexPath.row]];
    }
    return cell;
}

Upvotes: 0

Views: 69

Answers (1)

Hermann Klecker
Hermann Klecker

Reputation: 14068

if (_isFiltered) {
    ...
    cell.iconImageView.image = [UIImage imageNamed:[self.searchImagesArray objectAtIndex:indexPath.row]];
}
else
{
    ...
    cell.iconImageView.image = [UIImage imageNamed:[self.searchImagesArray objectAtIndex:indexPath.row]];
}

What is the point in the if clause when you execute the same code in both branches? I'd say that you schould do something different in the if and the else branch. E.g. fetch another name of the image from another array or so.

Something like

if (_isFiltered) {
    cell.itemNameLabel.text = [self.filteredTableData objectAtIndex:indexPath.row];
    cell.iconImageView.image = [UIImage imageNamed:[self.filteredImagesArray objectAtIndex:indexPath.row]];
}
else
{
    cell.itemNameLabel.text = [self.searchItemsArray objectAtIndex:indexPath.row];
    cell.iconImageView.image = [UIImage imageNamed:[self.searchImagesArray objectAtIndex:indexPath.row]];
}

Upvotes: 1

Related Questions