2

さて、長い間検索し、躊躇し、さらに検索した後、私はこれを理解できないようです.

SearchDisplayController があり、NSPredicate を使用して検索する 4 つの異なる配列があります。これは、各 UITableViewCell が 4 つの文字列 (タイトル、説明など) で構成されているためです。

私は今、次のように入力された4つの検索配列、search_title、search_descriptionなどを持っています:

- (void)filterContentForSearchText:(NSString*)searchText 
                             scope:(NSString*)scope
{
    NSPredicate *resultPredicate = [NSPredicate 
                                    predicateWithFormat:@"SELF contains[cd] %@",
                                    searchText];

    self.search_category = [self.temp_category filteredArrayUsingPredicate:resultPredicate];
    self.search_producttitle = [self.price_producttitle filteredArrayUsingPredicate:resultPredicate];
    self.search_type = [self.price_type filteredArrayUsingPredicate:resultPredicate];
    self.search_description = [self.price_description filteredArrayUsingPredicate:resultPredicate];

NSLog (配列カウント) を使用すると、検索語を入力すると各配列の正しいカウントが得られるため、機能していることがわかります。

私のテーブルビューセルは次のようになります。

if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]){
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.nameLabel.text = [self.search_producttitle objectAtIndex:[[searchResults objectAtIndex:indexPath.row] integerValue]];
        cell.priceLabel.text = [self.search_price objectAtIndex:[[searchResults objectAtIndex:indexPath.row] integerValue]];
        cell.descrLabel.text = [self.search_description objectAtIndex:[[searchResults objectAtIndex:indexPath.row] integerValue]];
        cell.IDLabel.text = [self.search_type objectAtIndex:[[searchResults objectAtIndex:indexPath.row] integerValue]];
    }
    else{
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.nameLabel.text = [price_producttitle objectAtIndex:indexPath.row];
        cell.IDLabel.text = [price_type objectAtIndex:indexPath.row];
        cell.priceLabel.text = [price objectAtIndex:indexPath.row];
        cell.descrLabel.text = [price_description objectAtIndex:indexPath.row];

私が何をしようとしているのか、もうお分かりかもしれません。私は現在、古い配列から新しい配列を作成しています。これを検索配列と呼んでいます。ただし、これらの配列は互いに独立しています (1 つの配列が空で、もう 1 つの配列が researchresult で満たされている場合があります)。これらの検索配列に格納されているデータがどのインデックスからのものかを知る必要があります。search_producttitle のデータが price_producttitle (元の配列) のインデックス 1、3、および 4 に由来することがわかっている場合は、それらの数値を indexPath.row に使用して表示できます。少なくとも、セルを作成するときに objectAtIndex で使用する必要がある数値を含む searchResult 配列を作成することが、今のところ私の計画です。

セルが複数の配列で構成されているため、複数の配列内で検索している例が見つかりません。

誰かが私に良い方向へのヒント、または私が使用できる例を教えてもらえますか?

前もって感謝します。

プラストー

私が使用した参照:

SearchDisplayController は複数の配列を検索します

http://ygamretuta.me/2011/08/10/ios-implementing-a-basic-search-uisearchdisplaycontroller-and-interface-builder/

4

2 に答える 2

1

誰も私の質問に答えませんでしたが、私はそれを自分で理解しました。

実は難しすぎると思いました。私の場合は必要のないNSPredicateを使いたかったのです。最初にNSPredicateから文字列を取得してから、その文字列を比較したかったのです。現在、rangeofstringを使用しており、すべてのobjectatindexのループで、すべての配列に対してこれを実行しています。

配列で見つかった場合は、YESと報告すると、インデックスは別の配列に配置されます。この配列は、後でUITableViewを作成するときに使用され、indexPath.rowを置き換えます。

[self.searchResults removeAllObjects];

for (int i = 0; i < [temp_category count]; i++) {
    BOOL foundResult = FALSE;

    if ([[temp_category objectAtIndex:i] rangeOfString:searchText].location != NSNotFound) {
        foundResult = TRUE;
    }
    if ([[price_producttitle objectAtIndex:i] rangeOfString:searchText].location != NSNotFound) {
        foundResult = TRUE;
    }
    if ([[price_type objectAtIndex:i] rangeOfString:searchText].location != NSNotFound) {
        foundResult = TRUE;
    }
    if ([[price_description objectAtIndex:i] rangeOfString:searchText].location != NSNotFound) {
        foundResult = TRUE;
    }
    if (foundResult) {

        NSNumber *result = [NSNumber numberWithInt:i];
        if ([self searchResults] == nil) {
            NSMutableArray *array = [[NSMutableArray alloc] init];
            [self setSearchResults:array];
            [array release];
        }

            [searchResults addObject:result];

    }
}

NSLog (@"array = %i", [searchResults count]);
NSLog(@"%@",searchResults);

私はこの答えを信用しません。クレジットは次の場所に移動する必要があります:SearchDisplayController複数の配列を検索

このメソッドを使用して、-(void)searchtableviewに実装しました。これについては多くの質問はありませんが、これが私が見つけた唯一の適切な答えでした。

他の人にもこれが役立つことを願っています!!

于 2012-07-08T17:49:38.283 に答える
0
-(void)searchBar:(UISearchBar *)searchBar1 textDidChange:(NSString *)searchText
{
if ([searchText length]==0)
    {
      temp_array1 =[array_Main1 mutableCopy];
      temp_array2 =[array_Main2 mutableCopy];
      temp_array3 =[array_Main3 mutableCopy];
    }
 else
    {
      [temp_array1 removeAllObjects];
      [temp_array2 removeAllObjects];
      [temp_array3 removeAllObjects];
       int g = 0;
       for (int i=0; i< array_Main1.count;i++)
        {
            NSRange Range1 = [[array_Main1 objectAtIndex:i] rangeOfString:searchText options:NSCaseInsensitiveSearch];
            NSRange Range2 = [[array_Main2 objectAtIndex:i] rangeOfString:searchText options:NSCaseInsensitiveSearch];
            NSRange Range3 = [[array_Main3 objectAtIndex:i] rangeOfString:searchText options:NSCaseInsensitiveSearch];
            if (Range1.location != NSNotFound ||  Range2.location != NSNotFound ||  Range3.location != NSNotFound )
            {
                [temp_array1 addObject:[array_Main1 objectAtIndex:g]];
                [temp_array2 addObject:[array_Main2 objectAtIndex:g]];
                [temp_array3 addObject:[array_Main3 objectAtIndex:g]];

            }
            g++;
        }
    }
[table reloadData];
}
于 2014-08-05T09:04:00.067 に答える