How does UITableView implement the search box function?

Source: Internet
Author: User

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.

 

 

 

 

 

 

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.