Ashutosh
Ashutosh

Reputation: 5742

How to add two labels in cell.detailText.label of UITableViewCellStyleSubtitle?

Is there a way we can add two labels to cell.detailText.label of UITableViewCellStyleSubtitle. i want one of them left aligned and other one as right aligned.

Thanks,

Upvotes: 2

Views: 3978

Answers (4)

Moebius
Moebius

Reputation: 700

A pretty old question, but I figured I'd chime in anyway.

If you are not using the accessory view of your cell, you can create a UILabel and assign it to the accessoryView property. I have done this several times and works well.

Upvotes: 0

Benjamin Mayo
Benjamin Mayo

Reputation: 6679

This is not possible without cell subview customisation, as yinkou shows ... unless you concatenate the two strings together, and show both text strings on one line:

cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ - %@", firstString, secondString];

Upvotes: 2

yinkou
yinkou

Reputation: 5766

You can't add labels to the detailLabel, but you can add them to the contentView of the cell.

- (UITableViewCell *)tableView:(UITableView *)atableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [atableView dequeueReusableCellWithIdentifier:@"cell"];
if (!cell) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];

    UILabel *labelOne = [[UILabel alloc]initWithFrame:CGRectMake(20, 22, 140, 20)];
    UILabel *labelTwo = [[UILabel alloc]initWithFrame:CGRectMake(160, 22, 140, 20)];

    labelOne.text = @"Left";
    labelTwo.textAlignment = UITextAlignmentRight;
    labelTwo.text = @"Right";

    [cell.contentView addSubview:labelOne];
    [cell.contentView addSubview:labelTwo];
}


return cell;

}

Upvotes: 8

Cyprian
Cyprian

Reputation: 9453

Not quite sure if I understand you corretly but maybe you are looking for this:

UITableViewCellStyleValue1 A style for a cell with a label on the left side of the cell with left-aligned and black text; on the right side is a label that has smaller blue text and is right-aligned. The Settings application uses cells in this style. Available in iOS 3.0 and later. Declared in UITableViewCell.h.

Upvotes: 0

Related Questions