4

検索バーで TableView をフィルタリングしようとしています。TableView には、Imageview とラベルを持つカスタム プロトタイプ セルがあり、呼び出すことができます。ラベルは画像の下に隠れています。

それぞれに94個のアイテムを持つ2つの配列があります。何も検索されていない場合は正常に機能します。テーブルビューには、画像/セルが完全な順序で表示されます。

何かを検索すると、結果は常に適切な数のセルで返されます。ただし、画像自体はフィルタリングされません。これは、どのセルがラベルでフィルタリングされても、画像は常に同じであることを意味します。

これは私の UISearchBar コードの問題ですか?

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {    
    if (searchText.length == 0) {
       isFiltered = NO;    
     }    else    {
       isFiltered = YES;
       filteredHeroes = [[NSMutableArray alloc]init];

       for (NSString *str in totalHeroData)    {

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

           if (heroRange.location != NSNotFound) {
               [filteredHeroes addObject:str];

           }
       }    }    [self.HeroTableView reloadData]; }

他のリソースを提供していただければ幸いです。94 項目の配列が 2 つあることを思い出してください。それらをリンクする必要がある場合は、その方法も知りたいです。

ありがとうございました。

編集: Cellforrowatindexpath メソッドは次のとおりです。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath 
*)indexPath {    
       static NSString *CellIdentifier = @"heroTableCell";
       HeroTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];    
       if (cell == nil) {
       cell = [[HeroTableViewCell alloc]
               initWithStyle:UITableViewCellStyleDefault
               reuseIdentifier: CellIdentifier];    
       }
       // Configure the cell...
       cell.textLabel.text = [self.heroNames objectAtIndex:indexPath.row];            
       cell.imageView.image = [UIImage imageNamed:[self.heroImages objectAtIndex:indexPath.row]];

       return cell; 
   }

お問い合わせいただきありがとうございます。ご意見をお聞かせください :) ウェブの検索に約 10 時間費やしました :S

4

1 に答える 1

0

1 つの nsmutuable 配列を使用し、1 つのキー imagename と 1 つのキー Name を文字列として含む nsdictionary を追加する必要があります

[filteredHeroes addObject:[NSDictionary dictionaryWithObjectsAndKeys:name,@"Name",ImageName,@"Image", nil]];
cellForRowAtIndexPathで同じfilteredHeroesを使用し、キーNameを使用して同じ配列をフィルタリングし、TableDataをリロードして完了します。

于 2013-01-22T13:30:45.267 に答える