In iOS, search display controller is used to query uitableview on the foreground.

Source: Internet
Author: User

  1. Use the search display controller control

  2. Implementation

    Uisearchdisplaydelegate

  3. Define the data of all tables and filtered data, and filter tags.

    @ Property (strong,
    Nonatomic) nsarray * aroriginal;

    @ Property (strong,
    Nonatomic) nsarray * arfiltered;

    @ Property (readwrite,
    Nonatomic) bool issearching;

  4. Implemented in the filter input box activation call

    - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {    if(self.searchDisplayController.searchBar.text.length>0) {        self.isSearching=YES;        NSString *strSearchText = self.searchDisplayController.searchBar.text;        NSMutableArray *ar=[NSMutableArray array];        // correctly working ! Thanx for watching video !        for (NSDictionary *d in self.arOriginal) {            NSString *strData = [d valueForKey:@"name"];            if([strData rangeOfString:strSearchText].length>0) {                [ar addObject:d];            }        }        self.arFiltered=[NSArray arrayWithArray:ar];    } else {        self.isSearching=NO;    }    return YES;}

  5. Implementation when displaying each cell
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static NSString *CellIdentifier = @"Cell";        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];    if (cell == nil) {        cell = [[[NSBundle mainBundle] loadNibNamed:@"CCell" owner:self options:nil] objectAtIndex:0];    }    NSDictionary *dToAccess = (self.isSearching)?[self.arFiltered objectAtIndex:indexPath.row]:[self.arOriginal objectAtIndex:indexPath.row];        [(UILabel*)[cell viewWithTag:1] setText:[dToAccess valueForKey:@"name"]];    [(UILabel*)[cell viewWithTag:2] setText:[dToAccess valueForKey:@"price"]];    return cell;}
  6. Related project files

    Http://download.csdn.net/detail/remote_roamer/5890617

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.