Tuesday, 2 August 2016

Search Bar in UITableview in iOS using Objective C

Today we will learn how to use UISearch bar to give search option in UITableview in ios.

Steps:
1. Create a new Xcode Project Lets Say(DefaultSearchBarIntableView).
2. Go to MainStoryBoard and add a tableview, Searchbar and a label.
3. Now We need to set proper connection and delegate for UISearchBar and UITableView.
4. Check on Shows Cancel Button.
5. Now go to your ViewController.m and write the following code.


#import "ViewController.h"

@interface ViewController ()<UISearchBarDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSArray *searchResult;
@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;

@end
@implementation ViewController
{
    NSMutableArray *tableDataArray;
    BOOL searchEnabled;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    tableDataArray = [[NSMutableArray alloc]initWithObjects:@"Ram",@"Shyam",@"Mohan",@"nilesh", nil];
    self.searchResult = [NSMutableArray arrayWithCapacity:[tableDataArray count]];
    
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -  tableView Delagate Methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (searchEnabled) {
        return [self.searchResult count];
    }
    else{
        return [tableDataArray count];
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleTableItem";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }
    if (searchEnabled) {
        cell.textLabel.text = [self.searchResult objectAtIndex:indexPath.row];

    }
    else{
        cell.textLabel.text = [tableDataArray objectAtIndex:indexPath.row];
    }
       return cell;
}



#pragma mark - Search delegate methods

- (void)filterContentForSearchText:(NSString*)searchText
{
    NSPredicate *resultPredicate = [NSPredicate
                                    predicateWithFormat:@"SELF CONTAINS %@",
                                    searchText];
    
    _searchResult = [tableDataArray filteredArrayUsingPredicate:resultPredicate];
    [_tableView reloadData];
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    if (searchBar.text.length == 0) {
        searchEnabled = NO;
        [self.tableView reloadData];
    }
    else {
        searchEnabled = YES;
        [self filterContentForSearchText:searchBar.text];
    }
}

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
    searchEnabled = YES;
    [self filterContentForSearchText:searchBar.text];
}

-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
    [searchBar setText:@""];
    searchEnabled = NO;
    [_tableView reloadData];
    
}

@end

6. Now run the Code and see the desired outPut.


 7. You can download the whole source code from here.

Please do give Your valuable feedback.
Enjoy Coding.  :)

No comments:

Post a Comment

Ads Inside Post

Blogger Widget