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];