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

Posted

技术标签:

【中文标题】如何让我的 UITableViewCell 包含两个标签?【英文标题】:How can i make my UITableViewCell contain two labels? 【发布时间】:2010-05-25 20:01:47 【问题描述】:

我希望我的 UITableViewCell 看起来像下面的图像,其中似乎有两个标签。如果不继承 UITableViewCell 这可能吗?

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

【问题讨论】:

【参考方案1】:

UITableVieWCell 有不同的样式。见这里:

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

我想你想使用 UITableViewCellStyleValue1。

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

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

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

【讨论】:

【参考方案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;

如果您有任何问题,请告诉我。如果需要,我可以澄清一些事情。

【讨论】:

虽然将子视图添加到单元格的contentView 功能强大且有用,但对于回答特定问题而言,这些额外的工作是不必要的。有关符合提问者要求的单元格示例,请参阅表格视图单元格样式。【参考方案3】:

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

【讨论】:

【参考方案4】:

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

【讨论】:

以上是关于如何让我的 UITableViewCell 包含两个标签?的主要内容,如果未能解决你的问题,请参考以下文章

带有 UILabel 子视图缓存问题的 UITableViewCell

在自定义UITableViewCell中保持UIImageView的宽高比

如何清除包含在重复使用的自定义 UITableViewCell 中的 WKWebview?

如何让我的 UICollectionViewCell 阴影渗入我的主视图?

如何让我的模型接受两种不同的日期格式?

如何让我的两个音频播放器同时播放两首不同的歌曲?