[英]Search Bar showing wrong/mixed cell results
我的 iOS 項目有問題。 我正在使用搜索欄在我的自定義 tableview 中過濾我的數組以顯示搜索的匹配關鍵字。
它沒有顯示代碼錯誤,但顯然存在問題。
通常,我的 tableview 中有 20 多個項目,但是當我搜索時,它只顯示我的 tableview 通常沒有搜索的第一個單元格,該單元格的圖像完全相同,但它不是同一個單元格,因為每個單元格都有一個按鈕通過該單元格中的標簽顯示特定信息。 因此,當我搜索並出現結果時,它會顯示 tableview 中第一個單元格具有的圖像,但帶有正確單元格或匹配單元格的標簽或信息。
我不知道那可能是什么。 也許它保留了第一個單元格的圖像,因為它是一個自定義的 tableview 單元格,並且由於某種原因它沒有過濾出正確的圖像。 但奇怪的是,它顯示的是正確的單元格,因為顯示的是每個單元格的特定標簽,而不是第一個單元格與第一個單元格圖片的標簽。 我希望我說得有道理。
代碼:
截面行數
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;
搜索欄 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];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.