Panda Pig • Patty original or translated works. Welcome reprint, Reprint please indicate the source.
If you feel that the writing is not good please more advice, if you feel good please support a lot of praise. Thank you! Hopy;)
Previous searches for iOS were generally done using Uisearchdisplaycontroller, but since iOS 8.0, the controller has been marked as obsolete and we can use the iOS After 8.0 use a new search controller Uisearchcontroller to complete the search.
This post will cover the simple usage of the 2 search controllers above and compare their differences. Let ' t go!
Uisearchdisplaycontroller
With this controller we must add a uisearchdisplaydelegate protocol to the root controller and then complete several of its prescribed callback methods:
-(BOOL) Searchdisplaycontroller: (Uisearchdisplaycontroller *) Controller Shouldreloadtableforsearch String: (NSString*) searchstring{[_filterednames removeallobjects];if(searchstring. Length>0) {Nspredicate *predicate = [nspredicate predicatewithblock: ^BOOL(NSString*name,nsdictionary*B) {NsrangeRange = [name rangeofstring:searchstring Options:nscaseinsensitivesearch];returnRange. location!=Nsnotfound; }]; for(NSString*key in _keys) {Nsarray*matchs = [_names[key] filteredarrayusingpredicate:predicate]; [_filterednames Addobjectsfromarray:matchs]; } }return YES;} -(void) Searchdisplaycontroller: (Uisearchdisplaycontroller *) Controller Didloadsearchresultsta Bleview: (UITableView*) tableview{[TableView registerclass:[UITableViewCellClass] Forcellreuseidentifier:sectionstableid];}
The first method is called when the search keyword changes, the latter is called after the search table view has been loaded, and we can see that, as shown in the code above, we bind the ID of the UITableViewCell element of its table view and a specific ID after the search table view is loaded. This allows you to reuse existing cells. It is also important to note that the ID is the same as the cell ID of the chart in the root view controller, although the two table views are not the same.
We initialize the Uisearchdisplaycontroller in the didloadview of the root view controller using the following code:
UISearchBar *searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0032044)];tableView.tableHeaderView = searchBar;_searchController = [[UISearchDisplayController alloc]initWithSearchBar:searchBar contentsController:self];_searchController.delegateself;_searchController.searchResultsDataSourceself;
The above code first creates a searchbar and sets it to the header view of TableView, then creates a Uisearchdisplaycontroller object with the Searchbar parameter, and finally sets up its corresponding delegate.
Because there are 2 table views in the root view at this point, we are dealing with the related callback method to distinguish which table view is currently being processed:
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ if (tableView.tag1) { return _keys.count; }else{ return1; }}
We set the default table view for rendering data to 1, so we know which table view we need to work with. Other callbacks are similar, no longer one by one.
Uisearchcontroller
Let's take a look at how the new search class is used after iOS 8.0. First we also have to add a uisearchresultsupdating protocol, and implement a protocol method:
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController;
This method is similar to the method of the previous search class, but note that it does not return a value, which means that if you want to display search results, you must manually refresh the table view. Here is an example of this method:
-(void) Updatesearchresultsforsearchcontroller: (Uisearchcontroller *) searchcontroller{NSString*searchstring = _searchcontroller. Searchbar. Text; [_filterednames removeallobjects];if(searchstring. Length>0) {Nspredicate *predicate = [nspredicate predicatewithblock: ^BOOL(NSString*name,nsdictionary*B) {NsrangeRange = [name rangeofstring:searchstring Options:nscaseinsensitivesearch];returnRange. location!=Nsnotfound; }]; for(NSString*key in _keys) {Nsarray*matchs = [_names[key] filteredarrayusingpredicate:predicate]; [_filterednames Addobjectsfromarray:matchs]; } }Else{//Show all keys if search keyword is empty for(NSString*key in _keys) {Nsarray*matchs = _names[key]; [_filterednames Addobjectsfromarray:matchs]; } }UITableView*tableview = [ Self. ViewViewwithtag:1];//Refresh table View manually[TableView Reloaddata];}
Uisearchcontroller also has a different place from Uisearchdisplaycontroller, which is to determine whether the current table view is a way to search the table view:
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ if (_searchController.active) { return1; }else{ return _keys.count; }}
Okay, I'll take it back. To be precise, the former does not have a table view of its own, and the table view it uses is the default table view. That is, we must determine whether the current search is active from the default table view, or display the results after the search, otherwise the default rendered data is displayed.
Finally let's take a look at how to initialize the Uisearchcontroller:
_searchController = [[UISearchController alloc]initWithSearchResultsController:nil]; _searchController.searchResultsUpdater = self; _searchController.dimsBackgroundDuringPresentation = NO; _searchController.hidesNavigationBarDuringPresentation = NO; tableView.tableHeaderView = _searchController.searchBar;
You can see no need to create Uisearchbar objects yourself, Uisearchdisplaycontroller has helped you get it done.
Summarize
Finally, let's take a look at the differences between the above 2 types of search controllers:
- The former uses a separate table view, which does not have a table view, and its contents are rendered using the default table view.
- The former requires manual creation of the Uisearchbar object, which is not required by the
- The former needs to determine which table view is currently in the protocol callback of the representation diagram, while the latter simply determines whether the search is active
- They abide by the same protocol, the Protocol is not the same way
- The former is more complex, the latter simpler, and more reasonable from the composition of the class.
2 ways to search in iOS Uisearchdisplaycontroller and Uisearchcontroller