IOS uses Uisearchdisplaycontroller to search for features

Source: Internet
Author: User

Uisearchdisplaycontroller is a controller in iOS that handles the search function, which needs to be used in conjunction with Uisearchbar

The sample code is as follows:

1 //2 //WKROOTVIEWCONTROLLER.M3 //search function for table view4 //5 //Created by student on 14-10-20.6 //Copyright (c) 2014 Wukong. All rights reserved.7 //8 9 #import "WKRootViewController.h"Ten  One @interfaceWkrootviewcontroller () A  -@property (Strong, nonatomic) nsmutablearray*DataSource; -  the@property (Strong, nonatomic) nsmutablearray*Resultarrat; -  -  - @end +  - @implementationWkrootviewcontroller + { A     //table view for loading a data source atUITableView *_tableview; -      -Uisearchbar *_searchbar; -      -Uisearchdisplaycontroller *_searchcontrol; - } in- (ID) Initwithnibname: (NSString *) Nibnameornil Bundle: (NSBundle *) Nibbundleornil - { toSelf =[Super Initwithnibname:nibnameornil Bundle:nibbundleornil]; +     if(self) { -         //Custom Initialization the     } *     returnSelf ; $ }Panax Notoginseng  -- (void) Viewdidload the { + [Super Viewdidload]; A      the [self createui]; + [self createdatasource]; -     //Do any additional setup after loading the view. $ } $  -- (void) CreateDataSource - { the_datasource =[[Nsmutablearray alloc] init]; -_resultarrat =[[Nsmutablearray alloc] init];Wuyi      for(inti ='A'; I <='Z'; i++) { theNsmutablearray *section =[[Nsmutablearray alloc] init]; -          for(intj =1; J <=Ten; J + +) { WuNSString *str = [NSString stringWithFormat:@"%c-%d", I, j]; - [Section addobject:str]; About         } $ [_datasource addobject:section]; -     } - } -  A #pragmaMark-uitableviewdatasource +  the-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView - { $     //determine the current displayed table the     if(TableView! =_tableview) the         return 1; the     return_datasource.count; the } -  in-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section the { the     if(TableView! =_tableview) { About         return_resultarrat.count; the     } the     return[[_datasource objectatindex:section] count]; the } +  --(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath the {Bayi[TableView Registerclass:[uitableviewcellclass] Forcellreuseidentifier:@"Cell"]; the      theUITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:@"Cell"]; -     if(TableView! =_tableview) { -Cell.textLabel.text =[_resultarrat ObjectAtIndex:indexPath.row]; the}Else{ theCell.textLabel.text =[[_datasource objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]; the     } the     returncell; - } the  the- (void) didreceivememorywarning the {94 [Super didreceivememorywarning]; the     //Dispose of any resources the can be recreated. the } the 98  About #pragmaMark-createui -- (void) Createui101 {102_tableview = [[UITableView alloc] Initwithframe:cgrectmake (Ten, -, -, the) Style:uitableviewstyleplain];103_tableview.Delegate=Self ;104_tableview.datasource =Self ; the [Self.view Addsubview:_tableview];106     107_searchbar = [[Uisearchbar alloc] Initwithframe:cgrectmake (0,0, -, -)];108_searchbar.searchbarstyle =Uisearchbarstyleminimal;109_searchbar.Delegate=Self ; the [_tableview Settableheaderview:_searchbar];111     /* the first parameter: A Uisearchbar object for entering search content113 The second parameter: The Controller object that is provided to my tabular view data source, which must be the two protocol that implements the table the      */ the_searchcontrol =[[Uisearchdisplaycontroller alloc] Initwithsearchbar:_searchbar contentscontroller:self]; the //_searchcontrol.searchresultstableview117 //UILabel *label = [[UILabel alloc] Initwithframe:cgrectmake (0, 0,.)];118 //label.backgroundcolor =[uicolor Redcolor];119 //[_searchcontrol.searchresultstableview Settableheaderview:label]; -     //to set a delegate object for a table view that comes with _searchcontrol121 [_searchcontrol setsearchresultsdatasource:self];122 [_searchcontrol setsearchresultsdelegate:self];123 }124  the #pragmaMark-uisearchbardelegate126- (void) Searchbar: (Uisearchbar *) Searchbar textdidchange: (NSString *) SearchText127 { - [_resultarrat removeallobjects];129NSString *str = [NSString stringWithFormat:@"*%@*", SearchText]; theNspredicate *pred = [Nspredicate predicatewithformat:@"Self-like %@", str];131      for(Nsmutablearray *arrinch_datasource) { the          for(NSString *strincharr) {133             if([pred evaluatewithobject:str]) {134 [_resultarrat addobject:str];135             }136         }137     }138 }139 @end

IOS uses Uisearchdisplaycontroller to search for features

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.