Subclassing UITableViewCell

Today we are going to look at how to access the read only properties of the UITableViewCell UILabels in iOS5+. The solution, subclassing.

By subclassing, you will gain access to modify the frame, shadow, offset, and many more properties otherwise unavailable, as well as add your own objects to this view. Another great reason for subclassing is setting defaults for certain values, such as font and colour, so you do not have to redefine them every time you use a UITableView in your application.

For this example we will style one of the existing UILables, and add our own new label in. Firstly create a new header (StyledCell.h) and implementation file (StyledCell.m).

The Header

In the header file you want to define the interface as a subclass of UITableViewCell, and add the new label as a property.

@interface StyledCell : UITableViewCell {
	UILabel *newLabel;

@property (nonatomic, retain) UILabel *newLabel;


The Implementation

In the implementation file we want to define the layoutSubviews and initWithStyles functions. Our first step is to import the correct header files and synthesize the new label.

#import ‹UIKit/UIKit.h›
#import "StyledCell.h"

@implementation StyledCell

@synthesize newLabel = _newLabel;

Now we will override the layoutSubviews method.

-(void) layoutSubviews {
	[super layoutSubviews];
	self.textLabel.frame = CGRectMake(20, 2, 200, 50);
	self.textColor = [UIColor colorWithHexString:@"C4C5C5"];
	self.newLabel.frame = CGRectMake(230, 28, 40, 20);
	self.newLabel.textAlignment = [UITextAlignmentRight];

There is one important thing to note here. You must call the super method. As you can see, we have changed the size and position of the default textLabel attached to this view, and we have also defined the position and text alignment for our new label.

The final part of the implementation file is subclassing the initWithStyleMethod.

- (id) initWithStyle:(UITabelViewCellStyle)style reuseIdentifier:(NSStrng *) reuseIDentifier {
	if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
		self.newLabel = [[UILabel alloc] init];
		[self.contentView addSubview:self.newLabel];
	return self;

The important thing to note here is that you are initialising the new label we have created, and adding it to the UITabelViewCell View.

And that’s it, you have now subclassed a UITableViewCell.

Using The New Subclassed Cell

To use your new View, import the view into the header file of the class you would like to use it in, and then define the cell in the implementation file.

StyledCell *cell = [tableView dequeueReusableCellWithIdentifier:cellStyle];

if (cell == nil) {
	cell = [[StyledCell alloc] initWithStyle:UITableViewCellStyledValue1 reuseIdentifier:cellStyle];

If you have any questions or observations please feel free to leave a comment and I will get back to you as soon as I can.

We'd love to
hear from you

We've prepared a simple project planner to get started.

Lets get started

Or send us a

Phone: (07) 3878 1151

Address: Office 2, 59 Hardgrave Rd,
West End, Brisbane, 4101, QLD, Australia

Oops! We could not locate your form.