Wednesday, 20 January 2016

UISearchBar in Table View

UISearchBar in Table View


1) Set Your Storyboard Like This :




2) Write This Code in ViewController.h :



#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate , UISearchBarDelegate>
{
    int flgTemp;


    NSMutableDictionary *dic_data;

}
@property(strong,nonatomic)IBOutlet UITableView *tblobj;

@property(strong,nonatomic)IBOutlet UISearchBar* search ;

@property(strong,nonatomic)NSMutableArray *arrObj ;


@property(strong,nonatomic)NSMutableArray *arrImage ;

@property(strong,nonatomic)NSMutableArray *searchResults ;


@end




3) Write This Code in ViewController.m :

Note :- Here Two Method For Search :-

---> 1) For Search Between Word Also :-


#import "ViewController.h"


@interface ViewController ()

@end

@implementation ViewController
@synthesize arrObj,search,tblobj,searchResults, arrImage ;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    arrObj  = [[NSMutableArray alloc] initWithObjects:@"Accounting",@"Airport",@"Amusement Park",@"Aquarium",@"Art Gallery",@"Atm",@"Bakery",@"Bank",@"Bar",@"Beauty Salon",@"Bicycle Store",@"Book Store",@"Bowling Alley",@"Bus Station",@"Cafe",@"Campground",@"Car Dealer",@"Car Rental",@"Car Rrepair",@"Car Wash",@"Casino",@"Cemetery",@"Church",@"City Hall",@"Clothing Store",@"Convenience Store",@"Courthouse",@"Dentist",@"Department Store",@"Doctor",@"Electrician",@"Electronics Store",@"Embassy",@"Establishment",@"Finance",@"Fire Station",@"Florist",@"Food",@"Funeral Home",@"Furniture Store",@"Gas Station",@"General Contractor",@"Grocery Or market",@"Gym",@"Hair Care",@"Hardware Store",@"Health",@"Hindu Temple",@"Home Goods Store",@"Hospital",@"Insurance Agency",@"Jewelry Store",@"Laundry",@"Lawyer",@"Library",@"Liquor Store",@"Local Government Office",@"Locksmith",@"Lodging",@"Meal Lelivery",@"Meal Takeaway",@"Mosque",@"Movie rental",@"Movie Theater",@"Moving Company",@"Museum",@"Night Club",@"Painter",@"Park",@"Parking",@"Pet Store",@"Pharmacy",@"Physiotherapist",@"Place Of Worship",@"Plumber",@"Police",@"Post Office",@"Real Estate Agency",@"Restaurant",@"Roofing Contractor",@"Rv Park",@"School",@"Shoe Store",@"Shopping Mall",@"Spa",@"Stadium",@"Storage",@"Store",@"Subway Station",@"Synagogue",@"Taxi Stand",@"Train Station",@"Travel Agency",@"University",@"Veterinary Care",@"Zoo", nil];
    
 arrImage = [[NSMutableArray alloc] initWithObjects:@"1.png",@"2.png",@"3.png",@"4.png",@"5.png",@"6.png",@"7.png",@"8.png",@"9.png",@"10.png",@"11.png",@"12.png",@"13.png",@"14.png",@"15.png",@"16.png",@"17.png",@"18.png",@"19.png",@"20.png",@"21.png",@"22.png",@"23.png",@"24.png",@"25.png",@"26.png",@"27.png",@"28.png",@"29.png",@"30.png",@"31.png",@"32.png",@"33.png",@"34.png",@"35.png",@"36.png",@"37.png",@"38.png",@"39.png",@"40.png",@"41.png",@"42.png",@"43.png",@"44.png",@"45.png",@"46.png",@"47.png",@"48.png",@"49.png",@"50.png",@"51.png",@"52.png",@"53.png",@"54.png",@"55.png",@"56.png",@"57.png",@"58.png",@"59.png",@"60.png",@"61.png",@"62.png",@"63.png",@"64.png",@"65.png",@"66.png",@"67.png",@"68.png",@"69.png",@"70.png",@"71.png",@"72.png",@"73.png",@"74.png",@"96.png",@"95.png",@"75.png",@"76.png",@"77.png",@"78.png",@"79.png",@"80.png",@"81.png",@"82.png",@"83.png",@"84.png",@"85.png",@"86.png",@"87.png",@"88.png",@"89.png",@"90.png",@"91.png",@"92.png",@"93.png",@"94.png",nil];
    
    
        NSLog(@"%lu, %lu", (unsigned long)arrObj.count,(unsigned long)arrImage.count);
    

        dic_data=[[NSMutableDictionary alloc]initWithObjects:arrImage forKeys:arrObj];

    searchResults = [[NSMutableArray alloc] init];
   
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (flgTemp==1)
    {
        return searchResults.count ;
    }
    else
    {
       return arrObj.count ;

    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    if (flgTemp==1)
    {
        cell.textLabel.text = [searchResults objectAtIndex:indexPath.row];
    
        NSString *imgname=[dic_data valueForKey:[searchResults objectAtIndex:indexPath.row]];
    
        cell.imageView.image = [UIImage imageNamed:imgname];
    }
    else
    {
        cell.textLabel.text = [arrObj objectAtIndex:indexPath.row];
        
        cell.imageView.image = [arrImage objectAtIndex:indexPath.row];
    }
    return cell ;
    
}



- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
   
    flgTemp = 1 ;
    [searchResults removeAllObjects];
    
    NSString *mainArrayItem ;
    NSRange range;

    
    if (![searchText isEqualToString:@""])
    {
        for (int i=0; i< arrObj.count; i++)
        {
            mainArrayItem = [arrObj objectAtIndex:i];
            range =[mainArrayItem rangeOfString:searchText options:NSCaseInsensitiveSearch];
            
            if (range.length>0)
            {
                [searchResults addObject:[arrObj objectAtIndex:i]];
            }
        }
        [tblobj reloadData];
    }
    else
    {
        flgTemp = 0;
        [tblobj reloadData];
        
    }

}


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

@end


---> 2) For Search Only Starting Word :-


#import "ViewController.h"


@interface ViewController ()

@end

@implementation ViewController
@synthesize arrObj,search,tblobj,searchResults ;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    arrObj  = [[NSMutableArray allocinitWithObjects:@"Accounting",@"Airport",@"Amusement Park",@"Aquarium",@"Art Gallery",@"Atm",@"Bakery",@"Bank",@"Bar",@"Beauty Salon",@"Bicycle Store",@"Book Store",@"Bowling Alley",@"Bus Station",@"Cafe",@"Campground",@"Car Dealer",@"Car Rental",@"Car Rrepair",@"Car Wash",@"Casino",@"Cemetery",@"Church",@"City Hall",@"Clothing Store",@"Convenience Store",@"Courthouse",@"Dentist",@"Department Store",@"Doctor",@"Electrician",@"Electronics Store",@"Embassy",@"Establishment",@"Finance",@"Fire Station",@"Florist",@"Food",@"Funeral Home",@"Furniture Store",@"Gas Station",@"General Contractor",@"Grocery Or market",@"Gym",@"Hair Care",@"Hardware Store",@"Health",@"Hindu Temple",@"Home Goods Store",@"Hospital",@"Insurance Agency",@"Jewelry Store",@"Laundry",@"Lawyer",@"Library",@"Liquor Store",@"Local Government Office",@"Locksmith",@"Lodging",@"Meal Lelivery",@"Meal Takeaway",@"Mosque",@"Movie rental",@"Movie Theater",@"Moving Company",@"Museum",@"Night Club",@"Painter",@"Park",@"Parking",@"Pet Store",@"Pharmacy",@"Physiotherapist",@"Place Of Worship",@"Plumber",@"Police",@"Post Office",@"Real Estate Agency",@"Restaurant",@"Roofing Contractor",@"Rv Park",@"School",@"Shoe Store",@"Shopping Mall",@"Spa",@"Stadium",@"Storage",@"Store",@"Subway Station",@"Synagogue",@"Taxi Stand",@"Train Station",@"Travel Agency",@"University",@"Veterinary Care",@"Zoo"nil];
    
    searchResults = [[NSMutableArray allocinit];
   
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (flgTemp==1)
    {
        return searchResults.count ;
    }
    else
    {
       return arrObj.count ;

    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if (cell == nil)
    {
        cell = [[UITableViewCell allocinitWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    if (flgTemp==1)
    {
         cell.textLabel.text = [searchResults objectAtIndex:indexPath.row];    }
    else
    {
        cell.textLabel.text = [arrObj objectAtIndex:indexPath.row];
    }
    return cell ;
    
}

-(void)filterContantForSearchText:(NSString *)searchText scope:(NSString *)scope
{
    
    
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@",searchText];
    
    NSArray *tempArray = [arrObj filteredArrayUsingPredicate:predicate];
    
    searchResults = [NSMutableArray arrayWithArray:tempArray];
    
    [tblobj reloadData];
}

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    searchBar.showsCancelButton = YES;
    return YES;
}

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
    searchBar.showsCancelButton = NO;
    return YES;
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    if ([searchText isEqualToString:@""]||searchText==nil )
    {
        flgTemp = 0 ;
        [tblobj reloadData];
    }
    else
    {
        flgTemp = 1 ;
        [self filterContantForSearchText:searchText scope:@"Al"];
    }

}


- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar
{
    [searchBar resignFirstResponder];
}


- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    
    if([text isEqualToString:@"\n"])
    {
        [searchBar resignFirstResponder];
        return NO;
    }
    return YES;
}


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

@end
















No comments:

Post a Comment