How does UITableView implement the search box function?
1. Define the following two variables and declare them as attributes.
UISearchBar * searchBar;
UISearchDisplayController * searchDc;
2. Initialize the two controls in loadView.
-(Void) loadView <br/>{< br/> // create the search bar and search display controller here <br/> self. searchBar = [[UISearchBar alloc] initWithFrame: CGRectMake (0.0f, 0.0f, 320366f, 44.0f)]; <br/> self. searchBar. tintColor = [UIColor colorWithRed: 0.8f green: 0.8f blue: 0.8f alpha: 1.0f]; <br/> self. searchBar. autocorrectionType = UITextAutocorrectionTypeNo; <br/> self. searchBar. autocapitalizationType = UITextAutocapitalizationTypeNone; <br/> self. searchBar. keyboardType = UIKeyboardTypeAlphabet; <br/> self. searchBar. hidden = NO; <br/> self. searchBar. placeholder = [NSString stringWithCString: "Enter the text to be searched" encoding: NSUTF8StringEncoding]; <br/> self. tableView _. tableHeaderView = self. searchBar; </p> <p> self. searchDc = [[UISearchDisplayController alloc] initWithSearchBar: self. searchBar contentsController: self] autorelease]; <br/> self. searchDc. searchResultsDataSource = self; <br/> self. searchDc. searchResultsDelegate = self; <br/> [self. searchDc setActive: NO]; </p> <p>}
3. In-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section
In the function, perform special processing on the search controller.
-(Nsinteger) tableview :( uitableview *) tableview numberofrowsinsection :( nsinteger) section <br/>{< br/> If (tableview = tableview _) <br/>{< br/> // if it is a normal tableview <br/> // return the number of normal cells <br/>}< br/> else <br/ >{< br/> // search for the display controller <br/> STD:: String strtofind = [self. searchbar. text utf8string]; <br/> // then, use strtofind to filter the results that match the search criteria and <br/> // return the number of cells that match the criteria. <Br/>}< br/>}
4. In-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath
, Return different cell data based on whether it is a search controller.
5. In-(void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath
And so on.