Tag Archive for 'UITableViewCell'

UITableViewCell with detail label

Creating some UITableViewCells with a main label and a secondary detail label (as we allready knew from the e.g. settings menu on the iPhone) to show some kind of a selected value isn’t very complicated but annoying anyway. The ways to do that were either to create a label as subview programmatically or you used the Interface Builder to provide a nib file including the cell.

This common task was greatly facilitated by the introduction of the new iPhone SDK 3.0, which now supports a few styles for UITableViewCells (see UITableViewCellStyle for more information). The following example shows how to create such a simple table cell:

//create a cell with the style UITableViewCellStyleValue1
UITableViewCell *cell = [[UITableViewCell alloc]
			 initWithStyle:UITableViewCellStyleValue1
			 reuseIdentifier:@"yourReuseIdentifier"];
 
//the main label will be shown on the left side
[cell.textLabel	setText:@"mainLabel"];
 
//the detail label will be shown on the right side
//in blue color, right-aligned and in a smaller font size
[cell.detailTextLabel setText:@"detailLabel"];

The result of this will be something like that:

tableviewcell_with_detail