Here's what we want to achieve. This effect is changed on the basis of the previous custom table view.
1. Drag the search bar and search display to the Viewcontroller. Do not add search Bar.
2. Modify the Viewcontroller header file
CPP Code
- #import <UIKit/UIKit.h>
- @interface Ikrboyviewcontroller4:uiviewcontroller
- {
- Nsarray *dataarr; //To display data for a table view
- Nsarray *alldataarr; //Store all data and assign search results to Dataarr by keyword search
- }
- @property (Weak, nonatomic) Iboutlet Uisearchbar *searchbar;
- @end
#import <UIKit/UIKit.h> @interface ikrboyviewcontroller4:uiviewcontroller{ nsarray *dataarr;//data for displaying table views Nsarray *alldataarr;//stores all data and assigns search results to Dataarr} @property (weak, nonatomic) Iboutlet uisearchbar *searchbar by keyword search ; @end
3. Modify the custom method Inittableviewdata. Hiding Scopebar is an issue that takes into account the iphone's display height. may be determined at its discretion.
CPP Code
- -(void) inittableviewdata{
- NSBundle *bundle = [NSBundle mainbundle];
- NSString *plistpath = [bundle pathforresource:@"User_head" oftype:@ "plist"];
- Alldataarr = [[Nsarray alloc] initwithcontentsoffile:plistpath];
- Dataarr = [Nsarray Arraywitharray:alldataarr];
- NSLog (@"table data Count =%d", [Alldataarr Count]);
- //Set Search bar Scopebar Hide
- [Self.searchbar Setshowsscopebar:no];
- [Self.searchbar SizeToFit];
- }
-(void) inittableviewdata{ nsbundle *bundle = [NSBundle mainbundle]; NSString *plistpath = [Bundle pathforresource:@ "User_head" oftype:@ "plist"]; Alldataarr = [[Nsarray alloc] initwithcontentsoffile:plistpath]; Dataarr = [Nsarray Arraywitharray:alldataarr]; NSLog (@ "table data Count =%d", [Alldataarr Count]); Set the search bar Scopebar hide [Self.searchbar setshowsscopebar:no]; [Self.searchbar SizeToFit];}
4. Add three event triggers for Searchbar
CPP Code
- The following three methods implement the search function of Searchbar
- Reload message to Table view data source when text content changes
- -(BOOL) Searchdisplaycontroller: (Uisearchdisplaycontroller *) controller shouldreloadtableforsearchstring: ( NSString *) SearchString
- {
- [Self filtercontentforsearchtext:searchstring scope:self.searchBar.selectedScopeButtonIndex];
- //yes case The table view can be Reloaded
- return YES;
- }
- Reload message to Table view data source when scope bar chooses to send changes
- -(BOOL) Searchdisplaycontroller: (Uisearchdisplaycontroller *) controller shouldreloadtableforsearchscope: ( Nsinteger) SearchOption
- {
- [Self filterContentForSearchText:self.searchBar.text scope:searchoption];
- //Yes Case table view can be Reloaded
- return YES;
- }
- Click the Cancel button for the event
- -(void) searchbarcancelbuttonclicked: (Uisearchbar *) Searchbar
- {
- //Query all
- [Self filtercontentforsearchtext:@"" scope:-1];
- }
The following three methods implement Searchbar's search function//When the text content changes, issue a reload message to the table view data source-(BOOL) Searchdisplaycontroller: (Uisearchdisplaycontroller *) Controller shouldreloadtableforsearchstring: (NSString *) searchstring{ [self filtercontentforsearchtext: SearchString Scope:self.searchBar.selectedScopeButtonIndex]; Yes, the table view can reload return yes; Reload message to Table view data source when the scope bar chooses to send changes-(BOOL) Searchdisplaycontroller: (Uisearchdisplaycontroller *) controller Shouldreloadtableforsearchscope: (Nsinteger) searchoption{ [self filtercontentforsearchtext: Self.searchBar.text Scope:searchoption]; Yes, the table view can reload return yes; Click the Cancel button event-(void) searchbarcancelbuttonclicked: (Uisearchbar *) searchbar{ //query all [self filtercontentforsearchtext:@ "" Scope:-1];}
5. Custom keyword search function
CPP Code
- A custom search method that searches from Alldataarr for elements that satisfy the search criteria and assigns the matched array to Dataarr, because Dataarr is the data source for the table view, so the table view's records change accordingly.
- -(void) Filtercontentforsearchtext: (nsstring*) searchtext scope: (nsuinteger) scope;
- {
- if ([SearchText length]==0)
- {
- //Query all
- Dataarr = [Nsarray Arraywitharray:alldataarr];
- NSLog (@"dataarr count =%d", [Dataarr Count]);
- return;
- }
- Nspredicate *scopepredicate;
- switch (scope) {
- Case 0:
- Scopepredicate = [Nspredicate predicatewithformat:@"(Self.itemname contains[c]%@) OR (Self.itemimagepath contains [C]%@) ", Searchtext,searchtext];
- NSLog (@"searchtext=%@", SearchText);
- Dataarr =[nsarray Arraywitharray:[alldataarr Filteredarrayusingpredicate:scopepredicate];
- Break ;
- Case 1:
- Scopepredicate = [nspredicate predicatewithformat:@"Self.itemname contains[c]%@", SearchText];
- Dataarr = [Nsarray arraywitharray:[alldataarr filteredarrayusingpredicate:scopepredicate]];
- Break ;
- Case 2:
- Scopepredicate = [nspredicate predicatewithformat:@"Self.itemimagepath contains[c]%@", SearchText];
- Dataarr =[nsarray Arraywitharray:[alldataarr Filteredarrayusingpredicate:scopepredicate];
- Break ;
- }
- }
A custom search method that searches from Alldataarr for elements that satisfy the search criteria and assigns the matched array to Dataarr, because Dataarr is the data source for the table view, so the table view's records change accordingly. -(void) Filtercontentforsearchtext: (nsstring*) searchtext scope: (nsuinteger) scope; {if ([SearchText length]==0) {//query all Dataarr = [Nsarray Arraywitharray:alldataarr]; NSLog (@ "Dataarr count =%d", [Dataarr Count]); Return } nspredicate *scopepredicate; Switch (scope) {Case 0:scopepredicate = [Nspredicate predicatewithformat:@ "(Self.itemname Contains[c] %@) OR (Self.itemimagepath contains[c]%@) ", Searchtext,searchtext]; NSLog (@ "searchtext=%@", SearchText); Dataarr =[nsarray Arraywitharray:[alldataarr Filteredarrayusingpredicate:scopepredicate]; Break Case 1:scopepredicate = [nspredicate predicatewithformat:@ "Self.itemname contains[c]%@", SearchText]; Dataarr = [Nsarray arraywitharray:[alldataarr filteredarrayusingpredicate:scopepredicate]]; Break CASE 2:scopepredicate = [nspredicate predicatewithformat:@ "Self.itemimagepath contains[c]%@", SearchText]; Dataarr =[nsarray Arraywitharray:[alldataarr Filteredarrayusingpredicate:scopepredicate]; Break }}
6. Modify the Cellforrowatindexpath method
CPP Code
- -(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath
- {
- static NSString *cellidentifier = @"Mytablecell";
- Mytableviewcell *cell = [TableView dequeuereusablecellwithidentifier:cellidentifier];
- //add code begin:important,for showing searching results
- //Do not judge the cell null value, will result in the search, can not find the corresponding identifier cell and error.
- if (cell = = nil) {
- //Search results using a simple table view cell's style, not a custom table view cell's style
- cell = [[Mytableviewcell alloc] Initwithstyle:uitableviewcellstylesubtitle reuseidentifier:cellidentifier];
- Nsuinteger row = [Indexpath row];
- Nsdictionary *rowdict = [Dataarr objectatindex:row];
- Cell.textLabel.text = [rowdict objectforkey:@"ItemName"];
- NSString *imagepath = [rowdict objectforkey:@"Itemimagepath"];
- Cell.imageView.image = [UIImage Imagenamed:imagepath];
- }
- //add code End
- Nsuinteger row = [Indexpath row];
- Nsdictionary *rowdict = [Dataarr objectatindex:row];
- Cell.label.text = [rowdict objectforkey:@"ItemName"];
- NSLog (@"Cell.label.text =%@", [rowdict objectforkey:@"ItemName"]);
- NSString *imagepath = [rowdict objectforkey:@"Itemimagepath"];
- Cell.image.image = [UIImage Imagenamed:imagepath];
- NSLog (@"cell.image.image =%@", ImagePath);
- Cell.accessorytype = Uitableviewcellaccessorydisclosureindicator;
- return cell;
- }
-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{static NSString *cellidentifier = @ "Mytablecell"; Mytableviewcell *cell = [TableView dequeuereusablecellwithidentifier:cellidentifier]; Add code begin:important,for showing searching results//does not determine the cell null value, will cause the search, the corresponding identifier can not find the cell and error. if (cell = = nil) {//Search results using a simple table view cell's style, not a custom table view cell's style cell = [[Mytableviewcell alloc] Initwithstyle:uit Ableviewcellstylesubtitle Reuseidentifier:cellidentifier]; Nsuinteger row = [Indexpath row]; Nsdictionary *rowdict = [Dataarr objectatindex:row]; Cell.textLabel.text = [rowdict objectforkey:@ "ItemName"]; NSString *imagepath = [rowdict objectforkey:@ "Itemimagepath"]; Cell.imageView.image = [UIImage Imagenamed:imagepath]; }//add code end Nsuinteger row = [Indexpath row]; Nsdictionary *rowdict = [Dataarr objectatindex:row]; Cell.label.text = [Rowdict Objectforkey:@ "ItemName"]; NSLog (@ "Cell.label.text =%@", [rowdict objectforkey:@ "ItemName"]); NSString *imagepath = [rowdict objectforkey:@ "Itemimagepath"]; Cell.image.image = [UIImage Imagenamed:imagepath]; NSLog (@ "cell.image.image =%@", ImagePath); Cell.accessorytype = Uitableviewcellaccessorydisclosureindicator; return cell;}
Add a search bar to the iOS table view