3

辞書を含む可変配列があります。その配列を表形式で表示しています。

今、テーブルビューに検索および表示コントローラーを実装したいと考えています。どのように?

任意の提案やコード..

ここで私の配列は、uitableview に "name" キーをアルファベット順に表示しています。

[
        {
            "name": "Fish",
            "description": "sdhshs",
            "colorCode": null,
        },
        {
            "name": "fry",
            "description": "sdhshs",
            "colorCode": null,
        },
        {
            "name": "curry",
            "description": "sdhshs",
            "colorCode": null,
        }
    ],
4

2 に答える 2

2

これがサンプルコードです

NSMutableArray *filteredResult; // this holds filtered data source
NSMutableArray *tableData; //this holds actual data source

-(void) filterForSearchText:(NSString *) text scope:(NSString *) scope
{
    [filteredResult removeAllObjects]; // clearing filter array
    NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:@"SELF.restaurantName contains[c] %@",text]; // Creating filter condition
    filteredResult = [NSMutableArray arrayWithArray:[tableData filteredArrayUsingPredicate:filterPredicate]]; // filtering result
}

デリゲート メソッド

-(BOOL) searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterForSearchText:searchString scope:[[[[self searchDisplayController] searchBar] scopeButtonTitles] objectAtIndex:[[[self searchDisplayController] searchBar] selectedScopeButtonIndex] ]];

    return YES;
}

-(BOOL) searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
{
    [self filterForSearchText:self.searchDisplayController.searchBar.text scope:
 [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];

    return YES;
}

NSPredicate 条件 "@"SELF.restaurantName contains[c] %@",text " restaurantName は、フィルタリングする必要があるプロパティ名です。データソース配列に NSString しかない場合は、 @"SELF contains[c] %@",text のように使用できます

フィルターが完了したら、それに応じてテーブルビュー デリゲートを実装する必要があります。このようなもの

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(tableView == [[self searchDisplayController] searchResultsTableView])
    {
        return [filteredResult count];
    }
    else
    {
        return [tableData count];

    }

}

フィルタリングされたテーブルビューであるか元のテーブルビューであるかをテーブルビューで比較し、それに応じてテーブルビューのデリゲートとデータソースを設定します。

上記のコードを機能させるには、XIB またはストーリーボードで使用している場合は、「検索バーと検索表示」オブジェクトを使用する必要があります。

于 2013-08-09T11:45:02.367 に答える
1

以下のサンプルコードを参照してください。

http://developer.apple.com/library/ios/samplecode/AdvancedTableSearch/Introduction/Intro.html#//apple_ref/doc/uid/DTS40013493

http://developer.apple.com/library/ios/samplecode/TableSearch/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007848

http://www.appcoda.com/how-to-add-search-bar-uitableview/

これらの例は、より良いアイデアを提供するかもしれません

于 2013-08-09T11:05:27.267 に答える