views:

113

answers:

3

I want my UITableViewCell to look like the image below where there seems to be two labels. Is this possible without subclassing UITableViewCell?

alt text

A: 

Not sure where u think you see 2 labels...you can set the UILabels number of lines property if you want more lines UILabel ref....Also there is a UITableViewCell type UITableViewCellStyleSubtitle which contains a detailTextLabel on top of the regular text labels in UITableCell, so you already have a built in cell with 2 text fields, here is a ref ref to UITableViewCell

Daniel
+1  A: 

You do not need to subclass a UITableViewCell in order to add content to it. Here could be a sample cell generation method with an extra label:

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];

        UILabel *secondLabel = [[UILabel alloc] initWithFrame:cell.textLabel.frame];
        secondLabel.textAlignment = UITextAlignmentRight;
        secondLabel.tag = 12345;

        [cell.contentView addSubview:secondLabel];
    }

    UILabel *second = [cell viewWithTag:12345];
    second.text = @"Second!";

    return cell;
}

Let me know if you have any questions. I can clarify some things if needed.

rickharrison
While adding subviews to the cell's `contentView` is powerful and useful, this extra work is unnecessary for answering the specific question. See the table view cell styles for examples of cells which match the asker's requirements.
Alex Reynolds
+4  A: 

There are different styles of UITableVieWCell. See here:

http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UITableViewCell_Class/Reference/Reference.html#//apple_ref/doc/c_ref/UITableViewCellStyle

I think you want to use UITableViewCellStyleValue1.

You can initialise your UITableViewCell with the relevant style:

http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UITableViewCell_Class/Reference/Reference.html#//apple_ref/occ/instm/UITableViewCell/initWithStyle:reuseIdentifier:

When you use a style that has two labels, you can use the textLabel and detailTextLabel properties to set them, respectively.

TheNextman