Search Controller


SearchViewController.h

#import <UIKit/UIKit.h>

@interface SearchViewController : UIViewController<UITableViewDelegate,UITableViewDataSource,UISearchResultsUpdating,UISearchBarDelegate>
@property (strong, nonatomic) IBOutlet UITableView *search_TableView;




//search
@property UISearchController*sc;
@end

SearchViewController.m

#import "SearchViewController.h"
#import "SearchTableViewCell.h"
@interface SearchViewController (){
    NSArray *name_Array,*category_Array,*image_Array;
    NSMutableDictionary *findImage_Dictionary,*findCategory_Dictionary;
    
    //search
    NSArray *searchedName_Array;
}

@end

@implementation SearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    name_Array = [[NSArray alloc]initWithObjects:@"Mahesh",@"Sukumar",@"Brami",@"Sathi",@"Arnab", nil];
    category_Array=[[NSArray alloc]initWithObjects:@"Hero",@"Director",@"Comedy",@"TV",@"Anchor", nil];
    image_Array=[[NSArray alloc]initWithObjects:[UIImage imageNamed:@"download.jpg"],[UIImage imageNamed:@"download-1.jpg"],[UIImage imageNamed:@"download-2.jpg"],[UIImage imageNamed:@"download-3.jpg"],[UIImage imageNamed:@"download-4.jpg"], nil];
    
    //search Array
    searchedName_Array=[[NSArray alloc]init];
   
    
    //table View
    _search_TableView.delegate=self;
    _search_TableView.dataSource=self;


    //search Bar programmatically
    self.sc=[[UISearchController alloc]initWithSearchResultsController:nil];
    self.search_TableView.tableHeaderView=self.sc.searchBar;
    self.sc.searchResultsUpdater=self;
    self.definesPresentationContext = YES;
    self.sc.dimsBackgroundDuringPresentation = NO;
    //    self.sc.searchBar.delegate=self;
    self.sc.searchBar.searchBarStyle = UISearchBarStyleMinimal;
    self.sc.searchBar.placeholder=@"Search Name";

    //dictionary
    [findImage_Dictionary removeAllObjects];
    [findCategory_Dictionary removeAllObjects];
    
    findImage_Dictionary=[[NSMutableDictionary alloc]initWithObjects:image_Array forKeys:name_Array];
    findCategory_Dictionary=[[NSMutableDictionary alloc]initWithObjects:category_Array forKeys:name_Array];
    
    
}

-(void)updateSearchResultsForSearchController:(UISearchController *)searchController{
    NSPredicate*predicate=[NSPredicate predicateWithFormat:@"SELF contains[c] %@",searchController.searchBar.text];
    
    [name_Array filteredArrayUsingPredicate:predicate];
    searchedName_Array=[name_Array filteredArrayUsingPredicate:predicate];
    NSLog(@"%@",searchedName_Array);
    [self.search_TableView reloadData];
}


# pragma mark - UITableViewControllerDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.sc.active==YES) {
        return searchedName_Array.count;
    }
    else{
        return name_Array.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *identifier=@"SearchCell";
    SearchTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell==nil) {
        cell=[[SearchTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    CALayer* layer = cell.layer;
    [layer setCornerRadius:8.0f];
    [layer setMasksToBounds:YES];
    [layer setBorderWidth:1.0f];
    if (self.sc.active==YES) {
        dispatch_async (dispatch_get_main_queue(), ^{
            cell.searchName_Label.text=[searchedName_Array objectAtIndex:indexPath.row];
            NSString *name=[searchedName_Array objectAtIndex:indexPath.row];
        
            cell.search_Image.image=(UIImage*) [findImage_Dictionary valueForKey:name];
            cell.searchCategory_Label.text=[findCategory_Dictionary valueForKey:name];

        });
    }
    else{
    
        dispatch_async (dispatch_get_main_queue(), ^{
            
            
            
            cell.searchName_Label.text=[NSString stringWithFormat:@"%@",[name_Array objectAtIndex:indexPath.row]];
            cell.searchCategory_Label.text=[NSString stringWithFormat:@"%@",[category_Array objectAtIndex:indexPath.row]];
            
            cell.search_Image.image = (UIImage*) [image_Array objectAtIndex:indexPath.row];
            
        });
        
    
    }
    
    return cell;
    
}


@end

SearchTableViewCell.h

#import <UIKit/UIKit.h>

@interface SearchTableViewCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UIImageView *search_Image;
@property (strong, nonatomic) IBOutlet UILabel *searchName_Label;
@property (strong, nonatomic) IBOutlet UILabel *searchCategory_Label;

@end

SearchTableViewCell.m

//not to do here

#import "SearchTableViewCell.h"

@implementation SearchTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end


Previous
Next Post »