1

説明: iOS 8 と互換性があり、Objective-Cを使用する新しいUISearchControllerでUITableView を検索する方法を教えてくれるチュートリアルが見つかりません。Parse.comクエリからのすべてのユーザーを含む「users」と呼ばれるNSMutableArrayが定義されており、View Controllerに「usersTable」と呼ばれる小さなテーブルビューがあります。usersTableのメソッドは次のとおりです。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"userCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    PFObject *tempObject = [users objectAtIndex:indexPath.row];

    cell.text= [tempObject objectForKey:@"username"];

    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSLog(@"users:%lu",[users count]);
    return [users count];
}

本文 1: ユーザーが UISearchController NSMutableArray と呼ばれるユーザーを使用して検索バーに入力したデータを除外し、それを「usersTable」に表示するだけです。分かりやすい例を挙げていただけると大変助かります。

結論: この時点でObjective-Cを使用したUISearchControllerの説明がないため、これについてどうすればよいかわからないので、誰か助けてください。助けていただければ幸いです。

ご協力いただきありがとうございます。

ところで:皆さんに私のためにこれをしてほしくありません。理解できるものが欲しいだけです。可能であれば、いくつかの指示または例。これにはSOの投稿がいくつかありますが、意味がありません。

4

1 に答える 1

4

コードと説明を探しているだけの場合は、Apple のサンプル プロジェクトを参照することをお勧めします。

https://developer.apple.com/library/ios/samplecode/TableSearch_UISearchController/Introduction/Intro.html

注意すべき主なことは、ストーリーボードからこれを行うことができないということです。代わりに、UISearchController現在検索が行われているビュー コントローラーに type のプロパティを作成します。次に、 のサブクラスであるか を含む新しいクラスを作成し、UITableViewControllerそれをプロパティのUITableViewとして設定しsearchResultsControllerますSearchController

注意すべきもう 1 つの重要な点は、これまでの とは異なり、UISearchDisplayController検索UISearchController結果をメイン テーブル ビューの上に表示するまったく異なるビュー コントローラーであるということです。(これはdefinesPresentationContext、コード内の変数によって実現されます)。

Apple は上記のリンクで必要なものをすべて提供していますが、コードの最も重要な部分といくつかのコメントを次に示します。

- (void)viewDidLoad {
    [super viewDidLoad];
    // results table controller is the same as what your search display controller used to be
    _resultsTableController = [[APLResultsTableController alloc] init];
    _searchController = [[UISearchController alloc] initWithSearchResultsController:self.resultsTableController];
    // this says 'tell this view controller when search updates are available'
    self.searchController.searchResultsUpdater = self;
    // this places the search bar atop the table view since it's hard to do this via storyboard
    [self.searchController.searchBar sizeToFit];
    self.tableView.tableHeaderView = self.searchController.searchBar;

    // we want to be the delegate for our filtered table so didSelectRowAtIndexPath is called for both tables
    self.resultsTableController.tableView.delegate = self;
    self.searchController.delegate = self;
    self.searchController.dimsBackgroundDuringPresentation = NO; // default is YES
    self.searchController.searchBar.delegate = self; // so we can monitor text changes + others

    // Search is now just presenting a view controller. As such, normal view controller
    // presentation semantics apply. Namely that presentation will walk up the view controller
    // hierarchy until it finds the root view controller or one that defines a presentation context.
    //
    self.definesPresentationContext = YES;  // know where you want UISearchController to be displayed
}
于 2015-03-31T13:22:01.017 に答える