【问题标题】:UISearchBar is not filtering custom cell images from UITableViewUISearchBar 没有从 UITableView 过滤自定义单元格图像
【发布时间】:2012-12-06 08:17:43
【问题描述】:

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

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

当我搜索某些东西时,结果总是会返回适当数量的单元格。但是,图像本身没有被过滤。这意味着无论标签过滤了哪些单元格,图像都将始终相同。

这是我的 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

【问题讨论】:

  • 你需要发布你的 cellforrowatindexpath 方法。
  • 已添加! cellforrowatindexpath 方法:)
  • 在 searchBar:textDidChange: 中,您正在过滤 filteredHeroes 数组中的数据,而在 cellForRowAtIndexPath 中,您正在使用 heroNames 和 heroImages。你能说出你是如何将这些联系起来的吗?
  • @Nick,您过滤了 filteredHeroes 而不是 heroNamesheroImages 数组。您还需要对其进行过滤。

标签: ios xcode uitableview uisearchbar


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-26
    • 1970-01-01
    • 1970-01-01
    • 2011-11-29
    • 1970-01-01
    • 1970-01-01
    • 2020-08-04
    • 1970-01-01
    相关资源
    最近更新 更多