UISearchBar in Xcode Storyboard

I want to create a searchBar for my table in the xcode storyboard, everything works for me and I have no error, just the search does not work.

I saw this two tutorials, but still not working. I can’t search. my tutorial:

-http://stackoverflow.com/questions/9897171/uisearchbar-implemented-with-storyboards -http://www.youtube.com/watch?v=IqDZHgI_s24 

Could you help me, Thanks in Advance!

Here is my code

CreateViewController.h

 #import <UIKit/UIKit.h> @interface CreateViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate> { // @property (weak, nonatomic) IBOutlet UISearchBar *searchBar; NSArray *datas; NSMutableArray *displayItems; IBOutlet UITableView * tableView; IBOutlet UISearchBar * searchbar; } @end 

CreateViewController.m

 #import "CreateViewController.h" @interface CreateViewController () @end @implementation CreateViewController - (id)initWithStyle:(UITableViewStyle)style { self = [super initWithStyle:style]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; datas = [NSMutableArray arrayWithObjects:@"Johan", @"Paul",@"George",@"Ringo", nil]; displayItems = [[NSMutableArray alloc] initWithArray:datas]; [super viewDidLoad]; } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. // eg self.myOutlet = nil; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationPortrait); } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. return [datas count]; } - (UITableViewCell *)tableView:(UITableView *)atableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell";//This is the identifier in storyboard UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if(!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"]; } cell.textLabel.text = [datas objectAtIndex:indexPath.row]; return cell; } -(void)searchbar:(UISearchBar *)searchbar textDidChange:(NSString *)searchText{ if([searchText length] == 0){ [displayItems removeAllObjects]; [displayItems addObjectsFromArray:datas]; }else{ [displayItems removeAllObjects]; for(NSString * string in datas){ NSRange r = [string rangeOfString:searchText options:NSCaseInsensitiveSearch]; if (r.location != NSNotFound){ [displayItems addObject:string]; } } } [tableView reloadData]; } #pragma mark - Table view delegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // Navigation logic may go here. Create and push another view controller. /* <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil]; // ... // Pass the selected object to the new view controller. [self.navigationController pushViewController:detailViewController animated:YES]; */ } @end 
+6
source share
5 answers

Use this iOS Quick Tip Tutorial: Filtering a UITableView with the Search Bar

http://code-ninja.org/blog/2012/01/08/ios-quick-tip-filtering-a-uitableview-with-a-search-bar/

 -(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)text { if(text.length == 0) { isFiltered = FALSE; } else { isFiltered = true; filteredTableData = [[NSMutableArray alloc] init]; for (Food* food in allTableData) { NSRange nameRange = [food.name rangeOfString:text options:NSCaseInsensitiveSearch]; NSRange descriptionRange = [food.description rangeOfString:text options:NSCaseInsensitiveSearch]; if(nameRange.location != NSNotFound || descriptionRange.location != NSNotFound) { [filteredTableData addObject:food]; } } } [self.tableView reloadData]; } 
+8
source

you can use NSPredicate for this function, change the search delegate method as below

  [datas release]; datas = nil; datas = [[NSMutableArray alloc] initWithArray: displayItems]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF like[c] %@",[NSString stringWithFormat:@"%@*",searchBar.text]]; [datas filterUsingPredicate:predicate]; [tableView reloadData]; 

check the sample code here

http://dl.dropbox.com/u/58723521/SearchSample.zip

+1
source

I agree, you need to change cellForRowAtIndexPath and numberOfRowsInSection to use the displayItems array, not the data array. Also check that you have set UISearchBarDelegate for your search bar.

+1
source

Do it like that

  • In your header file, declare arrays like this,

     NSMutableArray *datas; NSMutableArray *displayItems; 
  • Then change the initialization of the array in viewDidLoad , like this,

     datas = [[NSMutableArray alloc] initWithObjects:@"Johan", @"Paul",@"George",@"Ringo", nil]; displayItems = [[NSMutableArray alloc] initWithArray:datas]; 
  • Change the array in the cellForRowAt method ,

     cell.textLabel.text = [displayItems objectAtIndex:indexPath.row]; 
  • Then print the search string inside the textDidChange method using NSLog. If it does not print, something is wrong with the IBOutlet connection. If it prints, replace this method and see if it works,

     -(void)searchbar:(UISearchBar *)searchbar textDidChange:(NSString *)searchText{ if([searchText length] == 0){ }else{ [displayItems removeAllObjects]; for (int i=0; i<[datas count]; i++) { if ([searchText hasPrefix:[datas objectAtIndex:i]]) { [displayItems addObject:[datas objectAtIndex:i]]; } else { } } } [tableView reloadData]; } 

My sorting method is not the best in the world. But try to make your code work.

Change I think your delegate is not working. Try this method instead of the above.

 - (void) searchBarSearchButtonClicked:(UISearchBar*) theSearchBar { if([searchBar.text length] == 0){ arrayDisplayData = arrayOriginalData; }else{ [arrayDisplayData removeAllObjects]; for(NSString * string in arrayOriginalData){ NSRange r = [string rangeOfString:searchBar.text options:NSCaseInsensitiveSearch]; if (r.location != NSNotFound){ [arrayDisplayData addObject:string]; } } } [tblResults reloadData]; } 

Or that,

 - (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text 
0
source

You need to change cellForRowAtIndexPath to use the displayItems array, not the data array

 - (UITableViewCell *)tableView:(UITableView *)atableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell";//This is the identifier in storyboard UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if(!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"]; } cell.textLabel.text = [displayItems objectAtIndex:indexPath.row]; return cell; } 

And also change numberOfRowsInSection to use the number of displayItems

  - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. return [displayItems count]; } 
0
source

Source: https://habr.com/ru/post/914194/


All Articles