Search Bar showing wrong/mixed cell results

2019-07-31 03:24发布

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.

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.

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.

Code:

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];

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];

1条回答
叼着烟拽天下
2楼-- · 2019-07-31 03:44

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];
}
查看更多
登录 后发表回答