简体   繁体   English

使用UISearchController和Objective-C搜索UITableView而不是UITableViewController

[英]Searching a UITableView not a UITableViewController with UISearchController and Objective-C

Explanation: I cant find any tutorials telling me how to search a UITableView with the new UISearchController that is compatible for iOS 8 and uses Objective-C . 说明:我找不到任何教程可以告诉我如何使用与iOS 8兼容并使用Objective-C的新UISearchController搜索UITableView I have an NSMutableArray defined called 'users' which has all the users from my Parse.com query and I have a small table view in my View Controller called 'usersTable' the methods for my usersTable are as follows: 我有一个定义为'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];
}

Body 1: All I want to do is filter out the data that the user typed in the search bar using UISearchController NSMutableArray called users and then display it onto 'usersTable.' 正文1: 我要做的就是使用UISearchController NSMutableArray(称为users)过滤掉用户在搜索栏中键入的数据,然后将其显示在“ usersTable”上。 It'd be very helpful if you guys could give an example to understand. 如果你们可以举一个例子来理解,那将非常有帮助。

Conclusion: Could somebody please, please help me as I have no idea on how to go about this as there are no explanations for the UISearchController at this point in time with Objective-C. 结论: 有人可以,请帮助我,因为我不知道如何进行此操作,因为目前没有使用Objective-C的UISearchController解释。 I'd appreciate any help. 我将不胜感激。

Thanks for your help. 谢谢你的帮助。

BTW: I don't want you guys to do this for me. 顺便说一句:我不希望你们为我做这个。 I just want something I can understand. 我只想要我能理解的东西。 Maybe some instructions or an example if possible. 如果可能的话,也许是一些说明或示例。 There are some SO posts on this but they don't make any sense. 有一些SO帖子,但没有任何意义。

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: 如果您只是在寻找一些代码和解释,我建议在这里查看Apple的示例项目:

https://developer.apple.com/library/ios/samplecode/TableSearch_UISearchController/Introduction/Intro.html 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. 相反,您将在搜索当前所在的视图控制器上创建UISearchController类型的属性。 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. 然后,您将创建一个新类,该类可以是UITableViewController的子类,也可以具有UITableView ,并将其设置为SearchController属性的searchResultsController

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. 值得注意的另一点是,与过去的UISearchDisplayController不同, UISearchController是一个完全不同的视图控制器,可在主表视图的顶部显示搜索结果。 (This is achieved by the definesPresentationContext variable in the code). (这是通过代码中的definesPresentationContext变量实现的)。

While Apple provides you everything you need in the link above, here is the most important part of the code with some comments: 尽管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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM