ios 如何使用目标 c 实现 UISearchController
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32169796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to implement UISearchController with objective c
提问by Richard
I have an existing app, written in objective-c, with a table view.
我有一个现有的应用程序,用objective-c 编写,带有表格视图。
I am now trying to go back to this app and add a search bar to the table.
我现在正在尝试返回此应用程序并向表格中添加一个搜索栏。
The problem is that now there is the new UISearchController
protocol, there seems to be very little information online in how to implement this within objective-c - all tutorials and examples that I can find are all for Swift.
问题是现在有了新UISearchController
协议,网上关于如何在 Objective-c 中实现这一点的信息似乎很少——我能找到的所有教程和示例都是针对 Swift 的。
I have added the delegates to the .h file:
我已将委托添加到 .h 文件中:
UISearchBarDelegate, UISearchResultsUpdating
And I have the following code in viewDidLoad
, which works and adds a search bar:
我在 中有以下代码viewDidLoad
,它可以工作并添加一个搜索栏:
// Search controller
searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
searchController.searchResultsUpdater = self;
searchController.dimsBackgroundDuringPresentation = NO;
searchController.searchBar.delegate = self;
// Add the search bar
self.tableView.tableHeaderView = searchController.searchBar;
self.definesPresentationContext = YES;
[searchController.searchBar sizeToFit];
And that is as far as I have got!
这就是我所拥有的!
I would appreciate any pointers, example code or tutorials on how to implement the new UISearchController in an existing objective-c app tableview.
我将不胜感激关于如何在现有的 Objective-c 应用程序 tableview 中实现新的 UISearchController 的任何指针、示例代码或教程。
回答by Vicky Dhas
Initialise Following things as per steps mentioned.
按照提到的步骤初始化以下内容。
1) Protocol declaration in<UISearchBarDelegate, UISearchControllerDelegate, UISearchResultsUpdating>
in the .h interface class
1)<UISearchBarDelegate, UISearchControllerDelegate, UISearchResultsUpdating>
.h接口类中的协议声明
2) Declare following properties
2) 声明以下属性
//Fetch result controller
@property (nonatomic, strong) UISearchController *searchController;
//for the results to be shown with two table delegates
@property (nonatomic, strong) CLCustomerResultrowsItemsCellController *searchResultsController;
//this custom controller is only suppose to have number of rows and cell for row function of table datasource
3) For state restoration
3) 用于状态恢复
@property BOOL searchControllerWasActive;
@property BOOL searchControllerSearchFieldWasFirstResponder;
4) Initialise the code in this step in ViewDidload
4)在ViewDidload中初始化这一步的代码
_searchResultsController = [[CLChatContactsSearchResultController alloc] init];
_searchController = [[UISearchController alloc] initWithSearchResultsController:_searchResultsController];
self.searchController.searchResultsUpdater = self;
self.searchController.searchBar.placeholder = nil;
[self.searchController.searchBar sizeToFit];
self.contactsTableView.tableHeaderView = self.searchController.searchBar;
// we want to be the delegate for our filtered table so didSelectRowAtIndexPath is called for both tables
self.searchResultsController.tableView.delegate = self;
self.searchController.delegate = self;
self.searchController.dimsBackgroundDuringPresentation = YES; // 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
5) Use Button even to initiate the controller and past these functions for future usage if any see comments
5) 甚至使用 Button 来启动控制器并传递这些功能以备将来使用(如果有的话)查看评论
#pragma mark - UISearchBarDelegate
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
[searchBar resignFirstResponder];
}
#pragma mark - UISearchControllerDelegate
// Called after the search controller's search bar has agreed to begin editing or when
// 'active' is set to YES.
// If you choose not to present the controller yourself or do not implement this method,
// a default presentation is performed on your behalf.
//
// Implement this method if the default presentation is not adequate for your purposes.
//
- (void)presentSearchController:(UISearchController *)searchController {
}
- (void)willPresentSearchController:(UISearchController *)searchController {
// do something before the search controller is presented
}
- (void)didPresentSearchController:(UISearchController *)searchController {
// do something after the search controller is presented
}
- (void)willDismissSearchController:(UISearchController *)searchController {
// do something before the search controller is dismissed
}
- (void)didDismissSearchController:(UISearchController *)searchController {
// do something after the search controller is dismissed
}
6) On searching in text you get this callback
6)在文本中搜索你会得到这个回调
#pragma mark - UISearchResultsUpdating
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
// update the filtered array based on the search text
NSString *searchText = searchController.searchBar.text;
id <NSFetchedResultsSectionInfo> sectionInfo = [_fetchedResultsController.sections objectAtIndex:0];
if (searchText == nil) {
// If empty the search results are the same as the original data
self.searchResults = [sectionInfo.objects mutableCopy];
} else {
NSMutableArray *searchResults = [[NSMutableArray alloc] init];
NSArray *allObjects = sectionInfo.objects;
for (PhoneNumber *phoneMO in allObjects) {
if ([phoneMO.number containsString:searchText] || [[phoneMO.closrr_id filteredId] containsString:searchText] || [[phoneMO.contact.fullname lowercaseString] containsString:[searchText lowercaseString]]) {
[searchResults addObject:phoneMO];
}
}
self.searchResults = searchResults;
}
// hand over the filtered results to our search results table
CLCustomerResultrowsItemsCellController *tableController = (CLCustomerResultrowsItemsCellController *)self.searchController.searchResultsController;
tableController.filteredContacts = self.searchResults;
[tableController.tableView reloadData];
}
7) You have to declare the filteredContacts property in the Custom class that will fill the searched items .
7)您必须在将填充搜索项的自定义类中声明filteredContacts 属性。
8) Thats it , in did select row compare the table view if its the main controller or custom controller class table view and do the operation for the selected item.
8)就是这样,在选择行比较表视图,如果它是主控制器或自定义控制器类表视图,并为所选项目做操作。
Hope this is helpful.
希望这是有帮助的。