gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 18:11:49 25 4
gpt4 key购买 nike

解释:
我找不到任何教程告诉我如何搜索 UITableView 使用新的 UISearchController 与 iOS 8 兼容并使用 objective-C .我有一个 NSMutableArray 定义为“用户”其中包含我 Parse.com 查询中的所有用户,并且我的 View Controller 中有一个名为“usersTable”的小 TableView ,我的 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。我会很感激任何帮助。

谢谢你的帮助。

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

最佳答案

如果您只是在寻找一些代码和解释,我建议您在此处查看 Apple 的示例项目:

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

需要注意的主要事项是您不能从 Storyboard 中执行此操作。相反,您将创建一个类型为 UISearchController 的属性。在您的搜索当前所在的 View Controller 上。然后,您将创建一个新类,它是 UITableViewController 的子类。或有 UITableView就可以了,你将把它设置为searchResultsController您的SearchController属性(property)。

另一个需要注意的重要事情是,与过去不同的是 UISearchDisplayController , UISearchController是一个完全不同的 View Controller ,它在主 TableView 的顶部显示您的搜索结果。 (这是通过代码中的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
}

关于ios - 使用 UISearchController 和 Objective-C 搜索 UITableView 而不是 UITableViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29369357/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com