IOS development-UI (9) UITableView search function,-uiuitableview

Source: Internet
Author: User

IOS development-UI (9) UITableView search function,-uiuitableview

Knowledge point:

1. UITableView search function

<UITableViewDataSource, UITableViewDelegate, UISearchResultsUpdating, UISearchControllerDelegate>

======================================

UITableView search

1. UISearchController

Search Controller

@ Property (nonatomic, strong) UITableView * tableView; // table View

@ Property (nonatomic, strong) NSMutableArray * dataArr; // data source array

@ Property (nonatomic, strong) UISearchController * searchCtl; // search

 

 

2. Creation Method

-(Instancetype) initWithSearchResultsController :( UIViewController *) searchResultsController;

// Instantiate a ResultViewController * ctl = [ResultViewController new]; NSLog (@ "ctl = % p", ctl); // instantiate a search controller // parameter: you need to provide a controller self that displays the results. searchCtl = [[UISearchController alloc] initWithSearchResultsController: ctl];

 

 

3. @ property (nonatomic, assign) BOOL dimsBackgroundDuringPresentation;

Purpose: darken the background color during search. // deselect the darken effect self. searchCtl. dimsBackgroundDuringPresentation = NO;

 

4. Protocol proxies that can be followed

1) UISearchResultsUpdating

2) UISearchControllerDelegate

// Set proxy self. searchCtl. searchResultsUpdater = self; self. searchCtl. delegate = self;

 

5. Proxy Method

1)-(void) updateSearchResultsForSearchController :( UISearchController *) searchController;

Call time: when the input content in the search box changes

# Pragma mark-UISearchResultsUpdating // when the edit mode or the content in the search input box changes, the following method will be called back-(void) updateSearchResultsForSearchController :( UISearchController *) searchController {// The Controller object ResultViewController * resultCtl = (ResultViewController *) searchController that displays the search results. searchResultsController; NSLog (@ "resultCtl = % p", resultCtl); // clear the array [resultCtl. saveArr removeAllObjects]; for (NSString * name in self. dataArr) {// determines whether the name contains the searchBar. text if ([name containsString: searchController. searchBar. text]) {[resultCtl. saveArr addObject: name] ;}// refresh the UI [resultCtl. resultTableView reloadData];}

 

2)-(void) didDismissSearchController :( UISearchController *) searchController

Call time: When you exit the search mode, // exit the search mode-(void) didDismissSearchController :( UISearchController *) searchController {// switch to the non-search mode self. isSearch = NO; // refresh the UI [self. tableView reloadData];}

 

 

6. UISearchBar

Search box

// Set searchBar. The search input box is self. tableView. tableHeaderView = self. searchCtl. searchBar;

 

 

1) sizeToFit

Purpose: UIView changes the Frame size based on its own content.

// Automatically match the size of the content [self. searchCtl. searchBar sizeToFit];

 

2) placeholder

Purpose: Text prompt

// Set the text prompt self. searchCtl. searchBar. placeholder = @ "Enter the keyword ";

 

3) barTintColor

Purpose: the background color of the input box.

// Background color self. searchCtl. searchBar. barTintColor = [UIColor cyanColor];

 

Related 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.