Hello Here I shows how to filter data from the NSArray using searchBar value insert that time relevant data shows into UITableView.
First of all lets we get one UISearchBar for inserting value into them and then after according then string value relevant data display into below UITableView.It’s very easy and very sort trick to search data from NSArray.
Step 1: Put UISearchBar and give delegate to UIViewController’s finder.
Step 2: Put UITableView and give delegate and datasource toUIViewController’s finder.
Step 3 : Adding missing constraint click on red error and resolve all that.
Then assign appropriate IBOutlets to controller like below.
1 2 3 4 5 6 7 8 9 10 11 |
#import <UIKit/UIKit.h> @interface ViewController : UIViewController<UISearchBarDelegate,UITableViewDataSource,UITableViewDelegate>{ NSArray *arForAllCountryData; NSArray *arForCountryData; } @property (weak, nonatomic) IBOutlet UISearchBar *searchCntl; @property (weak, nonatomic) IBOutlet UITableView *tblForCountry; @end |
Now Delegate & Datasource UISearchBar and UITableView from storyboard to viewcontroller’s finder.
Step 4: Assign NSArray values.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.searchCntl.delegate = self; self.tblForCountry.delegate = self; arForCountryData = @[ @{ @"countryCode": @"AD", @"countryName": @"Andorra", @"currencyCode": @"EUR" }, @{ @"countryCode": @"AE", @"countryName": @"United Arab Emirates", @"currencyCode": @"AED" }, @{ @"countryCode": @"AF", @"countryName": @"Afghanistan", @"currencyCode": @"AFN" } ]; arForAllCountryData = arForCountryData; [self.tblForCountry reloadData]; } |
Step 5: Below is code for the Delegate methods of UITableView and UISearchBar
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
#pragma mark - UITableView Delegate -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return [arForCountryData count]; } -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; cell.selectionStyle = UITextBorderStyleNone; } cell.textLabel.text = [[arForCountryData objectAtIndex:indexPath.row] valueForKey:@"countryName"]; cell.detailTextLabel.text = [[arForCountryData objectAtIndex:indexPath.row] valueForKey:@"countryCode"]; return cell; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ } #pragma mark - #pragma mark UISearchBar Delegate methods - (void)searchBarTextDidBeginEditing:(UISearchBar *)sBar{ } - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { arForCountryData = arForAllCountryData; if([searchText length] > 0){ NSPredicate *predicate = [NSPredicate predicateWithFormat: @"countryName contains[cd] %@", searchText]; NSArray *matchingDicts = [arForCountryData filteredArrayUsingPredicate:predicate]; arForCountryData = [matchingDicts mutableCopy]; }else{ [self.searchCntl performSelector: @selector(resignFirstResponder) withObject: nil afterDelay: 0.1]; } [self.tblForCountry reloadData]; } - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{ [self.searchCntl resignFirstResponder]; } |
Leave a Reply