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.  :)

10 comments:

  1. Thank you very much! I tried a lot of tutorials and finally my app worked when I followed your guide. Thanks again

    ReplyDelete
  2. Excellent website. Lots of useful information here, thanks in your effort! . For more information please visit ios Online course

    ReplyDelete
  3. awesome post presented by you..your writing style is fabulous and keep update with your blogs.ios Online course Bangalore

    ReplyDelete
  4. Thanks to share.IOS is one of the leading and demanding to design the apps . where the process of software design apps it will be done using java PRogramming language thank you giving the information for all.
    ios Online course

    ReplyDelete
  5. Superb, it really helped, really easy to understand and implement. Thanks.

    ReplyDelete
  6. Great step by step solution, thanks for the help! ios Online Training Bangalore

    ReplyDelete
  7. This is one awesome blog article. Much thanks again. ios Online Training Hyderabad

    ReplyDelete
  8. I'm refactoring a ton of deprecated stuff this week and I looked at probably a half dozen blogs/tutorials on how to replace UISearchDisplayController with UISearchController. This one was by far the most helpful and useful. Thank you for taking the time to document this!

    ReplyDelete

Ads Inside Post

Blogger Widget