Search Bar showing wrong/mixed cell results

给你一囗甜甜゛ 提交于 2020-01-05 06:17:22

问题


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:


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


来源:https://stackoverflow.com/questions/40078850/search-bar-showing-wrong-mixed-cell-results

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!