1. Case Description: A table view with search function, 01,02,03
Fig. 01 FIG. 02 Fig. 03
2, main.storyboard,04
Figure 04
3,. h
#import<UIKit/UIKit.h>@interfaceCq23viewcontroller:uitableviewcontroller<uisearchbardelegate, uisearchdisplaydelegate>//Search Bar@property (Weak, nonatomic) Iboutlet Uisearchbar *Searchbar;//all teams assembled@property (nonatomic, strong) Nsarray *Listteams;//Team collection after query@property (nonatomic, strong) Nsmutablearray *Listfilterteams;//query by criteria, load the contents of the query, assign a value to Listfilterteams- (void) Filtercontentforsearchtext: (nsstring*) SearchText scope: (nsuinteger) scope;@end
4,. m
#import "CQ23ViewController.h"@interfaceCq23viewcontroller ()@end@implementationCq23viewcontroller- (void) viewdidload{[Super Viewdidload]; //Set Search bar Scopebar Hide[Self.searchbar Setshowsscopebar:no]; [Self.searchbar SizeToFit]; NSBundle*bundle =[NSBundle Mainbundle]; NSString*plistpath = [Bundle Pathforresource:@"Team"OfType:@"plist"]; //get all the data in a property list fileSelf.listteams =[[Nsarray alloc] initwithcontentsoffile:plistpath]; //initial access to all data[Self Filtercontentforsearchtext:@""scope:-1];}- (void) viewdidunload{[self setsearchbar:nil]; [Super Viewdidunload];}-(BOOL) shouldautorotatetointerfaceorientation: (uiinterfaceorientation) interfaceorientation{return(Interfaceorientation! =uiinterfaceorientationportraitupsidedown);}#pragmaMark Content filtering-(void) Filtercontentforsearchtext: (nsstring*) SearchText scope: (nsuinteger) scope; { if([SearchText length]==0) { //Query AllSelf.listfilterteams =[Nsmutablearray ArrayWithArray:self.listTeams]; return; } nspredicate*scopepredicate; Nsarray*Temparray; Switch(scope) { Case 0://EnglishScopepredicate = [Nspredicate predicatewithformat:@"self.name Contains[c]%@", SearchText]; Temparray=[Self.listteams filteredarrayusingpredicate:scopepredicate]; Self.listfilterteams=[Nsmutablearray Arraywitharray:temparray]; Break; Case 1: Scopepredicate= [Nspredicate Predicatewithformat:@"self.image Contains[c]%@", SearchText]; Temparray=[Self.listteams filteredarrayusingpredicate:scopepredicate]; Self.listfilterteams=[Nsmutablearray Arraywitharray:temparray]; Break; default: //Query AllSelf.listfilterteams =[Nsmutablearray ArrayWithArray:self.listTeams]; Break; }}#pragmaMark--uitableviewdatasource Protocol Method-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{return[Self.listfilterteams Count];}-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{StaticNSString *cellidentifier =@"Cellidentifier"; UITableViewCell*cell =[TableView Dequeuereusablecellwithidentifier:cellidentifier]; if(Cell = =Nil) {Cell=[[UITableViewCell alloc] Initwithstyle:uitableviewcellstylesubtitle reuseidentifier:cellidentifier]; } Nsuinteger Row=[Indexpath Row]; Nsdictionary*rowdict =[Self.listfilterteams Objectatindex:row]; Cell.textLabel.text= [Rowdict Objectforkey:@"name"]; Cell.detailTextLabel.text= [Rowdict Objectforkey:@"Image"]; NSString*imagepath = [Rowdict objectforkey:@"Image"]; ImagePath= [ImagePath stringbyappendingstring:@". PNG"]; Cell.imageView.image=[UIImage Imagenamed:imagepath]; Cell.accessorytype=Uitableviewcellaccessorydisclosureindicator; returncell;}#pragmaMark--uisearchbardelegate Protocol Method-(void) searchbarcancelbuttonclicked: (Uisearchbar *) searchbar{//When you click the Cancel button, check all[Self Filtercontentforsearchtext:@""scope:-1];}#pragmaMark-uisearchdisplaycontroller Delegate methods-(BOOL) Searchdisplaycontroller: (Uisearchdisplaycontroller *) Controller shouldreloadtableforsearchstring: (NSString *) searchstring{//Reload message to Table view data source When text content changes[self filtercontentforsearchtext:searchstring scope:self.searchBar.selectedScopeButtonIndex]; //Yes the table view can be Reloaded returnYES;}-(BOOL) Searchdisplaycontroller: (Uisearchdisplaycontroller *Controller shouldreloadtableforsearchscope: (nsinteger) searchoption{//Reload message to Table view data source when the scope bar selection changes[self filterContentForSearchText:self.searchBar.text scope:searchoption]; //Yes the table view can be Reloaded returnYES;}@end