IOS learning notes-Table view 3 (Search)

Source: Internet
Author: User

Add a search bar to the table view to search for data in the table view. To create and initialize a table view, you must follow the UITableViewDataSource protocol and set two arrays. One is to store the data of the objects to be searched in the table view, and the other is to store the searched data and re-load the table view. The search function must follow the UISearchBarDelegate protocol.

Add the protocol to the. h file and create the object:

@ Interface LinViewController: UIViewController
 
  
// Create a table view object @ property (retain, nonatomic) UITableView * mTableView; // create a search bar object @ property (retain, nonatomic) UISearchBar * mSearchBar; // create a variable array object, array 1 stores the table View data during initialization, array 2 stores the searched content, and uses @ property (retain, nonatomic) NSMutableArray * mArray1; @ property (retain, nonatomic) NSMutableArray * mArray2; @ end
 

Add the implementation method to the. m file:

@ Implementation LinViewController-(void) viewDidLoad {[super viewDidLoad]; // The table view initializes self. mTableView = [[UITableView alloc] initWithFrame: self. view. frame style: UITableViewStylePlain]; //? Sets the delegate object, table view delegate, and table drawing method self. mTableView. dataSource = self; // The search bar initializes self. mSearchBar = [[UISearchBar alloc] initWithFrame: CGRectMake (0, 0, self. mTableView. frame. size. width, 30)]; // Add the search bar to the table view header self. mTableView. tableHeaderView = self. mSearchBar; // Add the table View to the current view [self. view addSubview: self. mTableView]; //? Sets the delegate object and the delegate self in the search bar. mSearchBar. delegate = self; // array initialization, with 1-60 elements self. mArray1 = [[NSMutableArray alloc] initWithCapacity: 60]; self. mArray2 = [[NSMutableArray alloc] initWithCapacity: 60]; // looping, assigning values to array elements and storing them as strings for (int I = 0; I <60; I ++) {NSString * pTempString = [NSString stringWithFormat: @ "% d", I]; [self. mArray1 addObject: pTempString]; [self. mArray2 addObject: pTempString] ;}# pragma mark --- UITableViewDataSource --- protocol for loading table views-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {// obtain the number of rows of the table to be loaded in the table view. return [self. mArray2 count];}-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {// set a static string for tag, (static to prevent the creation of local variables multiple times, improving efficiency and saving memory) static NSString * identifer = @ "identifer"; // create a cell object (using the mechanism that indicates that the image can be reused) UITableViewCell * Cell = [tableView dequeueReusableCellWithIdentifier: identifer]; // if the cell is empty, create a new Cell if (nil = Cell) {Cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier: identifer];} // obtain the number of rows in the current table view NSInteger row = [indexPath row]; // pass the corresponding elements in the array to the text Cell of the view. textLabel. text = [self. mArray2 objectAtIndex: row]; return Cell;} # pragma mark --- UISearchBarDelegate ------- search method protocol-(void) searchBarSearchButtonClicked :( UISearchBar *) searchBar {// clear the elements of array 2 [self. mArray2 removeAllObjects]; // traverses all elements in array 1 for (NSString * str in self. mArray1) {// determines whether the string is equal to the character received in the search bar. if ([str hasPrefix: searchBar. text]) {// Add the searched element [self. mArray2 addObject: str] ;}// let SearchBar discard the first responder [self. mSearchBar resignFirstResponder]; // reload the view [self. mTableView reloadData];} // release the created object-(void) dealloc {[_ mArray1 release]; [_ mArray2 release]; [_ mTableView release]; [_ mSearchBar release]; [super dealloc];}-(void) didReceiveMemoryWarning {[super didreceivemorywarning]; // Dispose of any resources that can be recreated .} @ end



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.