gpt4 book ai didi

ios - 搜索栏显示错误/混合的单元格结果

转载 作者:行者123 更新时间:2023-11-29 11:53:35 25 4
gpt4 key购买 nike

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

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

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

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

代码:

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 的图像。

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

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 搜索最佳选项。

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

//在这里检查搜索是打开还是关闭,然后像这样更新你的数组

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

关于ios - 搜索栏显示错误/混合的单元格结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40078850/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com