ios - UISearchBar 未从 UITableView 中过滤自定义单元格图像

标签 ios xcode uitableview uisearchbar

我正在尝试使用搜索栏过滤 TableView。 TableView 有一个自定义原型(prototype)单元格,它有一个 Imageview 和一个标签,因此可以调用它。标签隐藏在图像下方。

我有两个数组,每个数组有 94 个项目。当没有搜索任何内容时,它工作正常。表格 View 以完美的顺序显示图像/单元格。

当我搜索某些内容时,返回的结果总是带有适当数量的单元格。但是,图像本身不会被过滤。这意味着无论按标签过滤什么单元格,图像都将始终相同。

这是我的 UISearchBar 代码的问题吗?

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {    
    if (searchText.length == 0) {
       isFiltered = NO;    
     }    else    {
       isFiltered = YES;
       filteredHeroes = [[NSMutableArray alloc]init];

       for (NSString *str in totalHeroData)    {

           NSRange heroRange = [str rangeOfString:searchText  options:NSCaseInsensitiveSearch];

           if (heroRange.location != NSNotFound) {
               [filteredHeroes addObject:str];

           }
       }    }    [self.HeroTableView reloadData]; }

我很乐意提供任何其他资源。请记住,我有两个包含 94 项的数组。如果我需要链接它们,我也想知道该怎么做。

谢谢。

编辑:这是 Cellforrowatindexpath 方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath 
*)indexPath {    
       static NSString *CellIdentifier = @"heroTableCell";
       HeroTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];    
       if (cell == nil) {
       cell = [[HeroTableViewCell alloc]
               initWithStyle:UITableViewCellStyleDefault
               reuseIdentifier: CellIdentifier];    
       }
       // Configure the cell...
       cell.textLabel.text = [self.heroNames objectAtIndex:indexPath.row];            
       cell.imageView.image = [UIImage imageNamed:[self.heroImages objectAtIndex:indexPath.row]];

       return cell; 
   }

感谢您的提问 - 让我知道您的想法 :) 我花了大约 10 个小时在网上搜索 :S

最佳答案

你应该使用一个 nsmutuable 数组并添加 nsdictionary,其中包含一个键 imagename 和一个键 Name 作为字符串

[filteredHeroes addObject:[NSDictionary dictionaryWithObjectsAndKeys:name,@"Name",ImageName,@"Image", nil]];
在 cellForRowAtIndexPath 中使用相同的 filteredHeroes 并使用键名称过滤相同的数组并重新加载 TableData 并完成。

关于ios - UISearchBar 未从 UITableView 中过滤自定义单元格图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13944599/

相关文章:

ios - 证书是否与 iOS 中的配置文件相关?

ios - UIScrollView 以编程方式滚动到底部

xcode - xcode 中没有错误消息吗?

ios - 如何通过单击按钮快速在 iOS 的 UITableview 中插入行?

iOS 8 自定义键盘

ios - 难以在Objective C中处理json格式

ios - 两个目标,两个同名的头文件如何将每个头文件用于正确的目标?

ios - Xcode 8.1 选择仅显示 iPhone 7 和 7s 的初始设备 View

xcode - 读取 UITableView Section Title 的字符串值

ios - 使用未解析的标识符 'cell'