Use UISearchDisplayController to Display Search Results

Source: Internet
Author: User

Once upon a time, I used UISearchBar. At that time, I saw that UISearchDisplayController was completely confused and I didn't know how to use it. This function is required in a recent app, so I wrote a Demo to explore it. Take notes now.


First, add a Search Bar and Search Display component to the visual controller of the story board:



Connect Outlets, including the search bar and SearchDisplayController:


<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + Signature = "brush: java;"> @ interface ViewController ()


Declare two arrays. The former stores the source data and the latter stores the searched data:
@property (strong, nonatomic) NSMutableArray *storeResults;@property (strong, nonatomic) NSMutableArray *results;


Initialization includes setting delegation and initializing arrays. The Code is as follows:
-(Void) viewDidLoad {[super viewDidLoad]; _ srcTableView. dataSource = self; _ srcTableView. delegate = self; _ searchBar. delegate = self; _ mySearchDisplayController. searchResultsDataSource = self; _ mySearchDisplayController. searchResultsDelegate = self; _ mySearchDisplayController. delegate = self; self. storeResults = [NSMutableArray arrayWithObjects: @ "Guangzhou", @ "Beijing", @ "Shanghai", @ "Shenzhen", @ "Hong Kong", @ "Guangdong ", @ "Beijing Normal University", @ "Peking University", @ "Xiangjiang", @ "coriander", @ "Hokkaido", @ "Huizhou", @ "Dongguan", @ "Hangzhou ", nil]; self. results = [NSMutableArray array];}


The following describes how to implement UISearchDisplayDelegate:
#pragma mark - UISearchDisplayDelegate- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {    [self filterContentForSearchText:searchString                               scope:[_searchBar scopeButtonTitles][_searchBar.selectedScopeButtonIndex]];        return YES;}-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption {    [self filterContentForSearchText:_searchBar.text                               scope:_searchBar.scopeButtonTitles[searchOption]];        return YES;}-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {    NSMutableArray *tempResults = [NSMutableArray array];    NSUInteger searchOptions = NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch;        for (int i = 0; i < _storeResults.count; i++) {        NSString *storeString = _storeResults[i];        NSRange storeRange = NSMakeRange(0, storeString.length);        NSRange foundRange = [storeString rangeOfString:searchText options:searchOptions range:storeRange];        if (foundRange.length) {            [tempResults addObject:storeString];        }    }    [self.results removeAllObjects];    [self.results addObjectsFromArray:tempResults];}

These methods are used to implement the search function. The matching rule is: whether the source string content contains or equal to the string content to be searched.

After the search results are displayed, we store the results in the results array. Next we implement the table delegate method to display the search results:
#pragma mark - UITableViewDataSource- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    if (tableView == _mySearchDisplayController.searchResultsTableView) {        return _results.count;    }    else {        return _storeResults.count;    }}- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{    return 1;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    static NSString *kCellIdentifier = @"Cell";    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];    if (!cell) {        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellIdentifier];    }        if (tableView == _mySearchDisplayController.searchResultsTableView) {        cell.textLabel.text = _results[indexPath.row];    }    else {        cell.textLabel.text = _storeResults[indexPath.row];    }        return cell;}#pragma mark - UITableViewDelegate- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {    if (tableView == _mySearchDisplayController.searchResultsTableView) {        NSLog(@"%@", _results[indexPath.row]);    }    else {        NSLog(@"%@", _storeResults[indexPath.row]);    }}
To identify whether the table content being displayed is search results or source data, tableView =

_ MySearchDisplayController. searchResultsTableView judgment.


The running result is as follows:

Components
3. Set the Delegate of the Search Bar and Search Display.
4. Implement the UISearchDisplayDelegate method. Search matching is completed here. After the search is completed, the search results are stored.
5. Implement the UITableViewDataSource and UITableViewDelegate methods to display the search results.

Of course, there are still many methods not implemented in the delegation. Here we only implement the basic search function, and more functions need to be explored in the future.
The Demo has been uploaded. You can refer to this article.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.