如何根据ios中搜索到的项目调整图像?

时间:2015-08-10 07:59:37

标签: ios objective-c uisearchbar

我使用UISearch栏来过滤表格中的列表,每件事都运行正常,但正确图像中的问题没有根据文字显示...请参见下图。任何帮助都会受到批评...... 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;
}

1 个答案:

答案 0 :(得分:1)

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

在两个分支中执行相同代码时,if子句有什么意义?我会说你可以在if和else分支中做一些不同的事情。例如。从另一个数组中获取图像的另一个名称。

这样的东西
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]];
}