Custom tableview with searchbar

UITableView, UISearchBar

FPOrderDetailsClass.h

@interface FPOrderDetailsClass : NSObject

@property(strong,nonatomic)NSString *orders;

@property(strong,nonatomic)NSString *firstname;

@property(strong,nonatomic)NSString *lastname;

@property(strong,nonatomic)NSString *orderdate;

@property(strong,nonatomic)NSString *city;

@property(strong,nonatomic)NSString *state;

@property(strong,nonatomic)NSString *price;

@property(strong,nonatomic)NSArray *productsList;

@end

FPOrderDetailsClass.m

@implementation FPOrderDetailsClass

@end

FPSearchCustomOrdersViewController.h

<UITableViewDataSource,UITableViewDelegate,UISearchDisplayDelegate>

NSArray *searchResults;

    NSArray *allArrays;

@property (strong, nonatomic) IBOutlet UITableView *myTableView;

FPSearchCustomOrdersViewController.m

#import “FPOrderDetailsClass.h”

#import “FPSearchCustomerOrderTableViewCell.h”

#import “FPSearchCustomerDetailViewController.h”

– (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    

    float width;

    float height;

    

    width = [[UIScreen mainScreen] bounds].size.width;

    height = [[UIScreen mainScreen] bounds].size.height;

    

    UIImage *backGroundImage = [self imageWithImage:[UIImage imageNamed:@”BackGround_For_All”] scaledToSize:CGSizeMake(width, height)];

    self.view.backgroundColor  = [UIColor colorWithPatternImage:backGroundImage];

    self.myTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

    

    

    FPOrderDetailsClass *custom1=[FPOrderDetailsClass new];

    custom1.orders=@”1″;

    custom1.firstname=@”Daniel”;

    custom1.lastname=@”Anthony”;

    custom1.orderdate=@”1/1/2016″;

    custom1.price=@”$19.90″;

    custom1.city=@”Dallas”;

    custom1.state=@”TX”;

     NSDictionary *dic1 = [[NSDictionary alloc]initWithObjectsAndKeys:@”Productimage1.png”,@”image”,@”SurgeShield Only”,@”name”,@”$9.95 plus tax”,@”detail”,nil];

    

     NSDictionary *dic2 = [[NSDictionary alloc]initWithObjectsAndKeys:@”Productimage2.png”,@”image”,@”Electronics Surge Protection Only”,@”name”,@”$9.95 plus tax” ,@”detail”,nil];

    

    NSArray *arr = [[NSArray alloc]initWithObjects:dic1,dic2, nil];

    custom1.productsList = arr;

    

    

    NSLog(@”%@”,custom1.productsList);

    

    

    FPOrderDetailsClass *custom2=[FPOrderDetailsClass new];

    custom2.orders=@”2″;

    custom2.firstname=@”James”;

    custom2.lastname=@”William”;

    custom2.orderdate=@”3/2/2016″;

    custom2.price=@”$19.90″;

    custom2.city=@”Columbus”;

    custom2.state=@”OH”;

    custom2.productsList=arr;

    

    FPOrderDetailsClass *custom3=[FPOrderDetailsClass new];

    custom3.orders=@”3″;

    custom3.firstname=@”David”;

    custom3.lastname=@”Andrew”;

    custom3.orderdate=@”5/3/2016″;

    custom3.price=@”$19.90″;

    custom3.city=@”New york”;

    custom3.state=@”NY”;

    custom3.productsList=arr;

    

    FPOrderDetailsClass *custom4=[FPOrderDetailsClass new];

    custom4.orders=@”4″;

    custom4.firstname=@”Jacob”;

    custom4.lastname=@”kevin”;

    custom4.orderdate=@”7/4/2016″;

    custom4.price=@”$19.90″;

    custom4.city=@”Hampton”;

    custom4.state=@”VA”;

    custom4.productsList=arr;

    

    FPOrderDetailsClass *custom5=[FPOrderDetailsClass new];

    custom5.orders=@”5″;

    custom5.firstname=@”Gabriel”;

    custom5.lastname=@”John”;

    custom5.orderdate=@”7/4/2016″;

    custom5.price=@”$19.90″;

    custom5.city=@”Miami”;

    custom5.state=@”FL”;

    custom5.productsList=arr;

    

    allArrays=[NSArray arrayWithObjects:custom1,custom2,custom3,custom4,custom5,nil];

    myTableView.delegate=self;

    [myTableView reloadData];

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    

    if (tableView == self.searchDisplayController.searchResultsTableView) {

        return [searchResults count];

        

    } else {

        return [allArrays count];

    }

    

}

– (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

    return 80;

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    static NSString *CellIdentifier = @”Cell”;

    

    

    FPSearchCustomerOrderTableViewCell *cell = (FPSearchCustomerOrderTableViewCell*)[self.myTableView dequeueReusableCellWithIdentifier:CellIdentifier];

    

    if (cell == nil)

    {

        cell = [[FPSearchCustomerOrderTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    }

    

    

    FPOrderDetailsClass *customcell= [FPOrderDetailsClass new];

    

    if (tableView == self.searchDisplayController.searchResultsTableView) {

        customcell=[searchResults objectAtIndex:indexPath.row];

    }

    else

    {

        customcell=[allArrays objectAtIndex:indexPath.row];

    }

    cell.OrdersLbl.text=customcell.orders;

    cell.FtrstName.text=customcell.firstname;

    cell.LastName.text=customcell.lastname;

    cell.DateLbl.text=customcell.orderdate;

    cell.CityLbl.text=customcell.city;

    cell.StateLbl.text=customcell.state;

    cell.PriceLbl.text=customcell.price;

    cell.selectionStyle=UITableViewCellSeparatorStyleNone;

    cell.selectionStyle=UITableViewCellSeparatorStyleNone;

    return cell;

}

– (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    if (tableView == self.searchDisplayController.searchResultsTableView) {

        //        [self performSegueWithIdentifier: @”FPSearchOrderDetailViewController” sender: self];

    }

    

}

– (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([segue.identifier isEqualToString:@”cellToDetailViewController”]) {

        FPSearchCustomerDetailViewController *destViewController = segue.destinationViewController;

        

        NSIndexPath *indexPath = nil;

        if ([self.searchDisplayController isActive]) {

            indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];

            destViewController.detaillist = [searchResults objectAtIndex:indexPath.row];

            

        } else {

            indexPath = [myTableView indexPathForSelectedRow];

            destViewController.detaillist = [allArrays objectAtIndex:indexPath.row];

        }

    }

    

}

– (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope

{

    NSPredicate *namepredicate = [NSPredicate predicateWithFormat:@”firstname contains[c] %@”, searchText];

    NSPredicate *ordernumberpredicate = [NSPredicate predicateWithFormat:@”orders contains[c] %@”, searchText];

    NSPredicate *orderdatepredicate = [NSPredicate predicateWithFormat:@”orderdate contains[c] %@”, searchText];

    NSPredicate *finalpredicate=[NSCompoundPredicate orPredicateWithSubpredicates:@[namepredicate,orderdatepredicate,ordernumberpredicate]];

    

    searchResults = [allArrays filteredArrayUsingPredicate:finalpredicate];

    

    

    

}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString

{

    [self filterContentForSearchText:searchString

                               scope:[[self.searchDisplayController.searchBar scopeButtonTitles]

                                      objectAtIndex:[self.searchDisplayController.searchBar

                                                     selectedScopeButtonIndex]]];

    

    return YES;

}

– (IBAction)HomeScreenBackBtn:(id)sender {

    [self.navigationController popViewControllerAnimated:YES];

}

– (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {

    //UIGraphicsBeginImageContext(newSize);

    // In next line, pass 0.0 to use the current device’s pixel scaling factor (and thus account for Retina resolution).

    // Pass 1.0 to force exact pixel size.

    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);

    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return newImage;

}

FPSearchCustomerDetailOrderTableViewCell.h

@interface FPSearchCustomerDetailOrderTableViewCell : UITableViewCell

@property (strong, nonatomic) IBOutlet UIImageView *ProductImage;

@property (strong, nonatomic) IBOutlet UILabel *ProductNameLbl;

@property (strong, nonatomic) IBOutlet UILabel *ProductCostLbl;

@end

FPSearchCustomerDetailOrderTableViewCell.m

@implementation FPSearchCustomerDetailOrderTableViewCell

– (void)awakeFromNib {

    // Initialization code

}

– (void)setSelected:(BOOL)selected animated:(BOOL)animated {

    [super setSelected:selected animated:animated];

    // Configure the view for the selected state

}

@end

FPSearchCustomerDetailViewController.h

#import “FPOrderDetailsClass.h”

<UITableViewDataSource,UITableViewDelegate>

NSMutableArray *productslistArray;

    NSMutableArray *productsImagesArray;

    NSMutableArray *productsCostArray;

@property (strong, nonatomic) IBOutlet UILabel *OrderLbl;

@property(strong,nonatomic)FPOrderDetailsClass *detaillist;

@property (strong, nonatomic) IBOutlet UIButton *SearchOrdersBackBtn;

@property (weak, nonatomic) IBOutlet UITableView *tblSearchCustomDetail;

#import “FPSearchCustomerDetailOrderTableViewCell.h”

– (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    self.OrderLbl.text=[self.detaillist valueForKey:@”orders”];

    

    float width;

    float height;

    

    width = [[UIScreen mainScreen] bounds].size.width;

    height = [[UIScreen mainScreen] bounds].size.height;

    

    UIImage *backGroundImage = [self imageWithImage:[UIImage imageNamed:@”BackGround_For_All”] scaledToSize:CGSizeMake(width, height)];

    self.view.backgroundColor  = [UIColor colorWithPatternImage:backGroundImage];

    

    self.tblSearchCustomDetail.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return [self.detaillist.productsList count];

}

– (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

    return 80;

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    FPSearchCustomerDetailOrderTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@”Cell”];

    if (cell==nil) {

        cell=[[FPSearchCustomerDetailOrderTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@”Cell”];

    }

    

    cell.ProductNameLbl.text=[[self.detaillist.productsList objectAtIndex:indexPath.row]objectForKey:@”name”];

    cell.ProductImage.image=[UIImage imageNamed:[[self.detaillist.productsList objectAtIndex:indexPath.row]objectForKey:@”image”]];

    

    cell.ProductCostLbl.text=[[self.detaillist.productsList objectAtIndex:indexPath.row]objectForKey:@”detail”];

    

    cell.selectionStyle=UITableViewCellSelectionStyleNone;

    cell.selectionStyle=UITableViewCellSeparatorStyleNone;

    

    return cell;

}

/*

#pragma mark – Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

– (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/

– (IBAction)SearchOrdersBackBtn:(id)sender {

    

    [self.navigationController popViewControllerAnimated:YES];

    

}

– (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {

    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);

    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return newImage;

}

FPSearchCustomerOrderTableViewCell.h

@interface FPSearchCustomerOrderTableViewCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UILabel *OrdersLbl;

@property (weak, nonatomic) IBOutlet UILabel *DateLbl;

@property (weak, nonatomic) IBOutlet UILabel *FtrstName;

@property (weak, nonatomic) IBOutlet UILabel *LastName;

@property (weak, nonatomic) IBOutlet UILabel *CityLbl;

@property (weak, nonatomic) IBOutlet UILabel *StateLbl;

@property (weak, nonatomic) IBOutlet UILabel *PriceLbl;

@end

FPSearchCustomerOrderTableViewCell.m

@implementation FPSearchCustomerOrderTableViewCell

– (void)awakeFromNib {

    // Initialization code

}

– (void)setSelected:(BOOL)selected animated:(BOOL)animated {

    [super setSelected:selected animated:animated];

    // Configure the view for the selected state

}

@end

Click here to download Custom tableview with searchbar

Leave a comment