Uploaded November 2012 | Updated September 2026, 2 weeks ago
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:
techytube.com/author/pavel
The delegate of a UITableView object must adopt the UITableViewDelegate protocol. Optional methods of the protocol allow the delegate to manage selections, configure section headings and footers, help to delete and reorder cells, and perform other actions.
Many methods of UITableViewDelegate take NSIndexPath objects as parameters and return values. UITableView declares a category on NSIndexPath that enables you to get the represented row index (row property) and section index (section property), and to construct an index path from a given row index and section index (indexPathForRow:inSection: method). Because rows are located within their sections, you usually must evaluate the section index number before you can identify the row by its index number.
One very important method is:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
and it used in Master-Detail applications, where we have a Main/Master view which contains a basic table with general information about the items that we want to display, and when we click on any of the items, it takes us to a Detailed view, which contains all the info about the selected item.
For more, please visit Apple's online documnation at:
developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDelegate_Protocol/Reference/Reference.html
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:
techytube.com/author/pavel
The delegate of a UITableView object must adopt the UITableViewDelegate protocol. Optional methods of the protocol allow the delegate to manage selections, configure section headings and footers, help to delete and reorder cells, and perform other actions.
Many methods of UITableViewDelegate take NSIndexPath objects as parameters and return values. UITableView declares a category on NSIndexPath that enables you to get the represented row index (row property) and section index (section property), and to construct an index path from a given row index and section index (indexPathForRow:inSection: method). Because rows are located within their sections, you usually must evaluate the section index number before you can identify the row by its index number.
One very important method is:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
and it used in Master-Detail applications, where we have a Main/Master view which contains a basic table with general information about the items that we want to display, and when we click on any of the items, it takes us to a Detailed view, which contains all the info about the selected item.
For more, please visit Apple's online documnation at:
developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDelegate_Protocol/Reference/Reference.html






![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)



