1

说明: 我找不到任何教程告诉我如何使用与iOS 8 兼容并使用Objective-C的新UISearchController搜索UITableView。我定义了一个名为“users”的 NSMutableArray,其中包含来自 Parse.com 查询的所有用户,并且我的视图控制器中有一个名为“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 在搜索栏中键入的数据,称为 users,然后将其显示到“usersTable”上。如果你们能举个例子来理解,那将非常有帮助。

结论: 有人可以请帮助我,因为我不知道如何解决这个问题,因为目前没有对 UISearchController 的解释与 Objective-C。我会很感激任何帮助。

谢谢你的帮助。

顺便说一句:我不希望你们为我这样做。我只是想要一些我能理解的东西。如果可能的话,可能会提供一些说明或示例。有一些关于此的帖子,但它们没有任何意义。

4

1 回答 1

4

If you're just looking for some code and an explanation to go off of, I'd recommend looking at Apple's sample project here:

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

The main things to note are that you can't do this from a storyboard. Instead, you will create a property of type UISearchController on the view controller where your search currently lives. Then, you'll create a new class that is either a subclass of UITableViewController or has a UITableView on it, and you will set that as the searchResultsController of the your SearchController property.

Another important thing to note is that unlike in the past with UISearchDisplayController, UISearchController is a whole different view controller that displays your search results on top of your main table view. (This is achieved by the definesPresentationContext variable in the code).

While Apple provides you everything you need in the link above, here is the most important part of the code with some comments:

- (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 回答