Monday, July 7, 2014

Customize Cell

Customize Cell

1. Create Empty application






2. Add an empty Interface








3.  Add a UITableViewcell to "CustomCell.xib"





4. Set name "CustomCell" in identifier 




     design the CustomCell

              


5. Create Class "CustomCell" under UITableViewCell 




6. Click the cell and set the Custom Class to "CustomCell" 






7. change the code in "CustomCell.h" like this

#import <UIKit/UIKit.h>

@interface CustomCell : UITableViewCell

@property (nonatomic, weak) IBOutlet UILabel *nameLabel;
@property (nonatomic, weak) IBOutlet UILabel *companyLabel;
@property (nonatomic, weak) IBOutlet UIImageView *profileImageView;

@end


7. Add UITableview to View controller and set delegate 

8. Change Code in View Controller Like this

ViewController.h
----------------------------------------------------------


#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
{
    IBOutlet UITableView *profileTableView;
    NSMutableArray *profileImageArray;
    NSMutableArray *nameArray;
    NSMutableArray *companyNameArray;
     
}

@property(strong,nonatomic) IBOutlet UITableView *profileTableView;
@property(strong,nonatomic) NSMutableArray *profileImageArray;
@property(strong,nonatomic) NSMutableArray *nameArray;
@property(strong,nonatomic) NSMutableArray *companyNameArray;

@end

ViewController.m
---------------------------------------------------------- 


import "ViewController.h"
#import "CustomCell.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize profileTableView,profileImageArray,companyNameArray,nameArray;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    profileImageArray =[[NSMutableArray alloc]initWithObjects:
                        @"Adrian.jpg",
                        @"Dinithe.jpg",
                        @"Jei.jpg",
                        @"Niron.jpg",
                        @"Jude.jpg",
                        @"",
                        @"",
                        @"",
                        @"",
                        @"",
                       
                        
                        nil];
   
    nameArray =[[NSMutableArray alloc]initWithObjects:
                @"Dhanushka Adrian",@"Dinithe",@"Nilakshan",@"Niron",@"Jude",
                @"Malsha",@"Kasun",@"Thisra",@"Suranga",@"Ishara",
                nil];
   
    companyNameArray =[[NSMutableArray alloc]initWithObjects:
                       @"Vburst Software",@"Vburst Software",@"Vburst Software",@"EZCrM",
                       @"Chello Dairy Products(Pvt)Ltd",@"Vburst Software",@"Vburst Software",
                       @"Vburst Software",@"Vburst Software",@"Vburst Software",
                       nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma Table Delegate
#pragma mark - Table Methods
- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section
{
   
    int nodeCount = [nameArray count];
    if (nodeCount == 0){
        return 1;
    }
    else{
        NSLog(@"%d",nodeCount);
        return  nodeCount;
    }
   
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"cellForRowAtIndexPath");
   
    NSString *uniqueIdentifier = @"CustomCell";
   
    CustomCell  *cell = nil;
   
    cell = (CustomCell *) [profileTableView dequeueReusableCellWithIdentifier:uniqueIdentifier];
   
   
   
   
    if(!cell)
    {
       
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil];
       
        for(id currentObject in topLevelObjects)
        {
            if([currentObject isKindOfClass:[CustomCell class]])
            {
                cell = (CustomCell *)currentObject;
                break;
            }
        }
    }
   
    cell.nameLabel.text         = [nameArray objectAtIndex:indexPath.row];
    cell.companyLabel.text      = [companyNameArray objectAtIndex:indexPath.row];
   
    cell.profileImageView.image = [UIImage imageNamed:[profileImageArray objectAtIndex:indexPath.row]];

    return cell;
   
   
}



-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath  *)indexPath{
   
    return 90.0f;
   
}


@end


Run The Project







No comments:

Post a Comment