如何让我的UITableViewCell包含两个标签?

时间:2010-05-25 20:01:47

标签: iphone objective-c cocoa-touch uitableview interface-builder

我希望我的UITableViewCell看起来像下面的图片,似乎有两个标签。如果没有子类化UITableViewCell,这可能吗?

alt text http://img31.imageshack.us/img31/2764/photoobp.jpg

4 个答案:

答案 0 :(得分:6)

UITableVieWCell有不同的风格。见这里:

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

我想你想使用UITableViewCellStyleValue1。

您可以使用相关样式初始化UITableViewCell:

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

当您使用具有两个标签的样式时,可以分别使用textLabel和detailTextLabel属性来设置它们。

答案 1 :(得分:2)

您无需为UITableViewCell创建子类以向其添加内容。这可以是带有额外标签的样本细胞生成方法:

- (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;
}

如果您有任何疑问,请与我们联系。如果需要,我可以澄清一些事情。

答案 2 :(得分:0)

不确定你认为你在哪里看到2个标签......如果你想要更多行UILabel ref,你可以设置UILabels行数属性....还有一个UITableViewCell类型UITableViewCellStyleSubtitle  其中包含一个在UITableCell中常规文本标签顶部的detailTextLabel,因此您已经有一个带有2个文本字段的内置单元格,这里是一个引用ref to UITableViewCell

答案 3 :(得分:0)

它不是2个标签,而是2个按钮,您需要在单元格的contentView视图中添加2个按钮。或者您可以创建页脚或标题查看并添加这两个按钮。