【发布时间】:2010-05-25 20:01:47
【问题描述】:
我希望我的 UITableViewCell 看起来像下面的图像,其中似乎有两个标签。如果不继承 UITableViewCell 这可能吗?
【问题讨论】:
标签: iphone objective-c cocoa-touch uitableview interface-builder
我希望我的 UITableViewCell 看起来像下面的图像,其中似乎有两个标签。如果不继承 UITableViewCell 这可能吗?
【问题讨论】:
标签: iphone objective-c cocoa-touch uitableview interface-builder
UITableVieWCell 有不同的样式。见这里:
https://developer.apple.com/documentation/uikit/uitableviewcell/cellstyle
我想你想使用 UITableViewCellStyleValue1。
您可以使用相关样式初始化您的 UITableViewCell:
https://developer.apple.com/documentation/uikit/uitableviewcell/1623276-init
当您使用具有两个标签的样式时,您可以分别使用 textLabel 和 detailTextLabel 属性来设置它们。
【讨论】:
您无需为 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 功能强大且有用,但对于回答特定问题而言,这些额外的工作是不必要的。有关符合提问者要求的单元格示例,请参阅表格视图单元格样式。
不确定你认为你在哪里看到 2 个标签...如果你想要更多行,你可以设置 UILabel 行数属性UILabel ref....还有一个 UITableViewCell 类型 UITableViewCellStyleSubtitle 它在 UITableCell 中的常规文本标签之上包含一个 detailTextLabel,因此您已经有一个带有 2 个文本字段的内置单元格,这是一个参考 ref to UITableViewCell
【讨论】:
它不是 2 个标签而是 2 个按钮,您需要在单元格的 contentView 视图中添加 2 个按钮。或者您可以创建一个页脚或页眉视图并添加这两个按钮。
【讨论】: