Uploaded August 2012 | Updated September 2026, 2 weeks ago
For more videos on technology, visit Techytube.com
By Pavel@Techytube, techytube.com/author/pavel
For more videos on technology, visit Techytube.com
By Pavel@Techytube, techytube.com/author/pavel




![How to create a custom cell programmatically in iOS
Subscribe to our channel here for notifications on new video trainings. For more videos on technology, visit our website at http://www.techytube.com.
By pavel@techytube, for more from this author visit:
http://www.techytube.com/author/pavel
A cell in a UITableView is an object of UITableViewCell class, and can have one of four styles, which are:
UITableViewCellStyleDefault, UITableViewCellStyleSubtitle, UITableViewCellStyleValue1, and UITableViewCellStyleValue2, and you can use those according to your apps specifications, or you can create your custom cells.
In this video tutorial we will create a custom cell programmatically. For that we have to do most of our work in the tableView:cellForRowAtIndexPath: method. First we need a dataSource to populate the cell with info, so declare a private property for that, in the VieController.m file, just after the #import statement:
@interface ViewController ()
@property (nonatomic, retain) NSMutableArray *dataSource;
@end
Then, in the viewDidLoad method allocate and initialise that:
self.dataSource = [NSMutableArray array];
[self.dataSource addObject:[NSArray arrayWithObjects:@20, @JUN, @7:00PM, @Toyota Center, @Houston, TX, @FROM, @$200, nil]];
[self.dataSource addObject:[NSArray arrayWithObjects:@6, @JUL, @8PM, @Frank Erwin Center, @Austin, TX, @FROM, @$350, nil]];
In the tableView:numberOfRowsInSection: method return self.dataSource.count.
In the tableView:cellForRowAtIndexPath: method write:
// Each subview in the cell will be identified by a unique tag
static NSUInteger const kDayLabelTag = 2;
static NSUInteger const kMonthLabelTag = 3;
// ... and so on
static NSString *CellIdentifier = @Cell;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UILabel *dayLabel;
UILabel *monthLabel;
// ... and so on
if (cell nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
// Define the frames for each component of the Cell
CGRect dayFrame = CGRectMake(5, 7, 64, 30);
CGRect monthFrame = CGRectMake(5, 35, 64, 16);
// ... and so on
// Adjust all the controls correspondingly
dayLabel = [[UILabel alloc] initWithFrame:dayFrame];
dayLabel.tag = kDayLabelTag;
dayLabel.font = [UIFont boldSystemFontOfSize:30.0f];
dayLabel.backgroundColor = [UIColor clearColor];
dayLabel.textColor = [UIColor darkGrayColor];
dayLabel.textAlignment = NSTextAlignmentCenter;
monthLabel = [[UILabel alloc] initWithFrame:monthFrame];
monthLabel.tag = kMonthLabelTag;
monthLabel.font = [UIFont boldSystemFontOfSize:16.0f];
monthLabel.backgroundColor = [UIColor clearColor];
monthLabel.textColor = [UIColor colorWithRed:0.0f/255.0f
green:140.0f/255.0f
blue:220.0f/255.0f
alpha:1.0f];
monthLabel.textAlignment = NSTextAlignmentCenter;
// ... and so on
// Add all created components to the new Cell
[cell.contentView addSubview:dayLabel];
[cell.contentView addSubview:monthLabel];
// ... and so on
}
else
{
// A reusable cell was available, so we just need to get
// a reference to the subviews using their tags
dayLabel = (UILabel *) [cell.contentView viewWithTag:kDayLabelTag];
monthLabel = (UILabel *) [cell.contentView viewWithTag:kMonthLabelTag];
}
NSArray *cellInfoArray = [self.dataSource objectAtIndex:indexPath.row];
dayLabel.text = [cellInfoArray objectAtIndex:0];
monthLabel.text = [cellInfoArray objectAtIndex:1];
return cell; How to create a custom cell programmatically in iOS](https://i.ytimg.com/vi/FTt7doXcYjc/mqdefault.jpg)





