Built-in UITableViewCell's Styles (UITableViewCellStyle)

UITableView is on of the most commonly used component when building an iOS application. It has some built-in styles that suit many scenarios. You might want to look into these styles before start customizing it.

There are 4 types of styles for the UITableViewCell, the enum representation of this is choices is UITableViewCellStyle here's how each choice looks.


    • UITableViewCellStyleDefault - default style with a label on the left side of the table. This is the default for a UITableView component.







    • UITableViewCellStyleValue1 - label on the left, and a detail label on the right side of the table.







    •  UITableViewCellStyleValue2 - a blue label on the left side (right aligned text), label on the right side (left aligned text).







    •  UITableViewCellStyleSubtitle - this is almost same with the default style except that it has a gray color smaller text under the label that function as the description or subtitle of the label.




To change the default style of a UITableViewCell, replace the styles enum in initWithStyle with any of the enum mentioned above, simple as that.

Example:


// ObjC version

- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}

// Configure the cell...
return cell;
}

// Swift version

override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell? {
let cellIdentifier = "Cell"
var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as UITableViewCell

if !cell {
cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: cellIdentifier)
}

// Configure the cell...
return cell
}

Comments

Post a Comment