显示错误/混合单元格结果的搜索栏 [英] Search Bar showing wrong/mixed cell results

查看:22
本文介绍了显示错误/混合单元格结果的搜索栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 iOS 项目有问题.我正在使用搜索栏在我的自定义 tableview 中过滤我的数组以显示搜索的匹配关键字.

I have a problem in my iOS project. I'm using a search bar to filter my array in my custom tableview to show the matching keywords of the search.

它没有显示代码错误,但显然存在问题.

It shows no code errors but obviously there's a problem.

通常在我的 tableview 中有超过 20 个项目,但是当我搜索时,它只显示我的 tableview 通常没有搜索的第一个单元格,该单元格的图像完全相同,但它不是同一个单元格,因为每个单元格有一个按钮,通过该单元格中的标签显示特定信息.因此,当我搜索并出现结果时,它会显示 tableview 中第一个单元格的图像,但带有正确单元格或匹配单元格的标签或信息.

Normally there's over 20+ items in my tableview but when I search, it only shows the first cell that my tableview normally has without searching, the same image for that cell to be exact, but it is not the same cell because each cell has a button showing specific information through a label in that cell. So when I search and a result comes out, it shows the image that the first cell in the tableview has but with the label or info of the correct cell or that matching cell has.

我不知道它可能是什么.也许它保留了第一个单元格的图像,因为它是一个自定义的 tableview 单元格,并且由于某种原因它没有过滤出正确的图像.但奇怪的是,它显示的是正确的单元格,因为显示的是每个单元格的特定标签,而不是第一个单元格与第一个单元格图片的标签.我希望我说得有道理.

I don't know what it could be. Maybe it's keeping the image of the first cell because it's a custom tableview cell and for some reason it's not filtering out to the correct image. But the weird part that it's the correct cell that is showing because the specific label that each cell has is showing and not the label that the first cell has with the first cell's picture. I hope I'm making sense.

代码:

numberOfRowsInSection

if (isFiltered) {
    return [filteredGamesArray count];
}
return [gamesArray count];

cellForRowAtIndexPath

Game * gameObject;

if (!isFiltered) {
    gameObject = [gamesArray objectAtIndex:indexPath.row];
}
else {
    gameObject = [filteredGamesArray objectAtIndex:indexPath.row];
}

return cell;

searchBar textDidChange

if (searchText.length == 0) {
    isFiltered = NO;
} else {
    isFiltered = YES;
    filteredGamesArray = [[NSMutableArray alloc] init];

    for (Game *game in gamesArray) {

        NSString *str = game.gameName;

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

        if (stringRange.location != NSNotFound) {
            [filteredGamesArray addObject:game];
        }
    }
}
[myTableView reloadData];

此外,我不会在我的应用中保留任何图像或文本.我使用 json/php/mysql 从服务器获取填充 tableview 单元格的所有信息,图像使用 URL.

Also, I don't keep any images or text in my app. I get all the info that populates the tableview cell from a server using json/php/mysql, with images I use a URL.

我在这里遗漏了一些东西.如果您需要更多详细信息,请告诉我.

I'm missing something here. If you need more details please let me know.

cellForRowAtIndexPath

// Loading Images
NSURL * imgURL = [[gamesArray objectAtIndex:indexPath.row] valueForKey:@"gameURL"];
[cell.myImageView setContentMode:UIViewContentModeScaleAspectFit];
[cell.myImageView sd_setImageWithURL:imgURL placeholderImage:[UIImage imageNamed:@"no-image.jpg"] options:SDWebImageRefreshCached];

推荐答案

使用 NSPredicate 搜索最佳选项.

Use NSPredicate for searching best option.

searchArray=[[NSArray alloc]init];
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF.YOURSEARCHKEY contains[c] %@", YOURTEXTFIELD.text];
NSLog(@"%@",resultPredicate);
searchArray = [ORIGNALARRAY filteredArrayUsingPredicate:resultPredicate];
NSLog(@"%@",searchArray);
[self.colorTableview reloadData];

//在这里检查搜索是打开还是关闭,然后像

// Here check if searching is on or off and just update your array like

if searching{
     NSURL * imgURL = [[searchArray objectAtIndex:indexPath.row] valueForKey:@"gameURL"];
    [cell.myImageView setContentMode:UIViewContentModeScaleAspectFit];
    [cell.myImageView sd_setImageWithURL:imgURL placeholderImage:[UIImage imageNamed:@"no-image.jpg"] options:SDWebImageRefreshCached];
}else{
    NSURL * imgURL = [[gamesArray objectAtIndex:indexPath.row] valueForKey:@"gameURL"];
    [cell.myImageView setContentMode:UIViewContentModeScaleAspectFit];
    [cell.myImageView sd_setImageWithURL:imgURL placeholderImage:[UIImage imageNamed:@"no-image.jpg"] options:SDWebImageRefreshCached];
}

这篇关于显示错误/混合单元格结果的搜索栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆