Ios.uikit.14.uitableview--Uisearchbar

Source: Internet
Author: User

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

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.