10

searchDisplayController検索する がありUITableViewます。

検索用語を入力すると、検索結果を含む別の用語が表示UITableViewされます。UITableViewただし、これを PLAIN ではなく GROUPEDにしたい(デフォルトのように)。

どうすればいいですか?

4

6 に答える 6

15

これは私にとってはうまくいきました(iOS 5.0):

self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
[self.searchController setValue:[NSNumber numberWithInt:UITableViewStyleGrouped]
        forKey:@"_searchResultsTableViewStyle"];
于 2012-03-20T23:27:48.257 に答える
14

私のように、単純な TableView があまりにも見苦しいと思われる場合は、SearchDisplayController の使用を放棄することもできます。

私はただ:

  • IBOutletに対して通常行うように、空のビューにsearchBarTableViewを挿入します。
  • ファイルの所有者を両方の代理人として選択しました。
  • 最初はセクションの数が 0 ([myTab count]) で、次に reloadData を使用し、今回は myTab に結果が入力されます。
  • [self.resultTableView reloadData];

ここでは、デリゲートから使用したすべてのメソッドを見つけることができます

@interface MyViewController : UIViewController <UIApplicationDelegate, UISearchBarDelegate> {

    IBOutlet UISearchBar *searchBar;
    IBOutlet UITableView *resultTableView;
}

@property (nonatomic, retain) IBOutlet UISearchBar *searchBar;
@property (nonatomic, retain) IBOutlet UITableView *resultTableView;

  //For your searchBar the 2 most important methods are
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBarClicked;
- (BOOL)searchBarTextDidEndEditing;


  //For your TableView the most important methods are in my case:

    //number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
    //HEADERS
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
    //different numbers of row by section
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
    //the cells themselves
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

@end

結局のところ、最も単純なソリューションが最善の場合が多いのです...

于 2011-05-20T03:28:20.770 に答える
3

これは私のために働く:

UISearchDisplayController を拡張するクラスを作成します。

@interface RVSearchDisplayController : UISearchDisplayController
@end

@implementation RVSearchDisplayController

-(UITableView *) searchResultsTableView {
    [self setValue:[NSNumber numberWithInt:UITableViewStyleGrouped]
            forKey:@"_searchResultsTableViewStyle"];
    return [super searchResultsTableView];
}

@end

UISearchDisplayController次に、IB を使用してテーブルに a を追加しRVSearchDisplayController、Identity Inspector でそのカスタム クラスを に変更します。

于 2011-08-19T14:43:41.317 に答える
1

UISearchDisplayController のサブクラスを作成して、searchResultsTableView を検索可能にすることができます。

任意の .h ファイルに次を追加します。

@interface YourUISearchDisplayController : UISearchDisplayController {
   UITableView * searchResultsTableView;
}

@property (nonatomic) UITableView * searchResultsTableView;

@end;

次に、UISearchDisplayController の代わりに YourUISearchDisplayController を使用します。

注: (非アトミック、保持)、(非アトミック、割り当て)、または (非アトミック、コピー) を使用する必要がある場合があります。よくわからない

于 2010-09-26T05:39:02.360 に答える
0

メソッドを呼び出さずにテーブルビューインスタンス変数に直接アクセスする-searchResultsTableViewため、オーバーライドは機能しません。UISearchDisplayController

の指定された初期化子は、インスタンス変数を設定UISearchDisplayControllerするプライベートメソッドであるように見えます。このインスタンス変数は、検索結果テーブルビューの作成に使用されます。パブリック初期化子はこのメソッドを呼び出し、を渡します。-initWithSearchBar:contentsController:searchResultsTableViewStyle:_searchResultsTableViewStyleUITableViewStylePlain

プライベート指定イニシャライザーを直接呼び出すか、インスタンス変数を設定すると、アプリケーションがApp Storeから拒否される可能性が高いため、代わりにパブリックイニシャライザーをオーバーライドして呼び出してみてください。

[self setValue:[NSNumber numberWithInt:UITableViewStyleGrouped]
        forKey:@"searchResultsTableViewStyle"];
于 2011-08-03T14:39:28.937 に答える
0

searchResultsTableViewプロパティが であるため、これは不可能ですreadonly

于 2010-09-26T05:24:52.660 に答える