Sheehan Alam
Sheehan Alam

Reputation: 60919

How can i make my UITableViewCell contain two labels?

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 http://img31.imageshack.us/img31/2764/photoobp.jpg

Upvotes: 2

Views: 6126

Answers (4)

TheNextman
TheNextman

Reputation: 12566

There are different styles of UITableVieWCell. See here:

https://developer.apple.com/documentation/uikit/uitableviewcell/cellstyle

I think you want to use UITableViewCellStyleValue1.

You can initialise your UITableViewCell with the relevant style:

https://developer.apple.com/documentation/uikit/uitableviewcell/1623276-init

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

Upvotes: 6

Thomas Decaux
Thomas Decaux

Reputation: 22691

Its not 2 labels but 2 buttons, you need to add 2 buttons in contentView view of the cell. Or you can create a footer or header View and add these 2 buttons.

Upvotes: 0

rickharrison
rickharrison

Reputation: 4846

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.

Upvotes: 2

Daniel
Daniel

Reputation: 22405

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

Upvotes: 0

Related Questions