UISearchBar未从UITableView中筛选自定义单元格图像



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

我有两个数组,每个数组中有94个项目。当什么都不搜索时,它工作得很好。表格视图以完美的顺序显示图像/单元格。

当我搜索某个东西时,结果总是带有适当数量的单元格。但是,图像本身不会被过滤。这意味着,无论标签过滤了什么细胞,图像都将始终相同。

这是我的UISearch条形码的问题吗?

-(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

您应该使用一个nsmutable数组,并添加包含一个密钥imagename和一个密钥Name的nsdictionary作为字符串

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

最新更新