【问题标题】:Width of detailTextLabel on cell with UITableViewCellStyleValue2 style具有 UITableViewCellStyleValue2 样式的单元格上的 detailTextLabel 的宽度
【发布时间】:2011-04-19 12:04:42
【问题描述】:

我是堆栈溢出和可可触摸开发的新手。

我正在尝试使用带有多行 detailTextLabel 的 UITableViewCellSytleValue2 构建 UITableViewCell。

因此我实现了 tableView:heightForRowAtIndexPath: 像这样:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

 if (indexPath.row != 4) {
  return tableView.rowHeight;
 }

 NSString *text = toDoItem.note;
 UIFont *font = [UIFont systemFontOfSize:[UIFont systemFontSize]];
 CGSize withinSize = CGSizeMake(167, 1000);
 CGSize size = [text sizeWithFont:font constrainedToSize:withinSize lineBreakMode:UILineBreakModeCharacterWrap];

 CGFloat height = size.height + 26;

    return height;    
}

我发现 detailTextLabel 的宽度大约为 167 像素,但想准确了解它并且不喜欢硬编码(考虑到方向变化)。

我尝试访问 detailTextLabel 的框架或边界,但它的宽度返回 0.0。

感谢您的帮助!

【问题讨论】:

  • 我使用 200px 字体大小为 15。这对我来说很好。

标签: cocoa-touch uitableview multiline


【解决方案1】:

创建自定义单元

【讨论】:

  • 谢谢你的回答,但实际上我只是不想:-)。我无法想象无法计算出该标签的宽度,因为它显然具有固定宽度。
【解决方案2】:

您可以在 UITableViewCell 的自定义实现中定义一组静态变量,当且仅当未设置时,它会在 layoutSubviews 中设置这些类型的参数。请务必先致电[super layoutSubviews]

这样您就可以继承设备的默认设置,而且您永远不必怀疑。
AFAIK,单元格宽度为self.contentView.frame.size.width。您可以使用self.textLabel.frame.size.widthself.detailTextLabel.frame.size.widthself.accessoryView.frame.size.width + 在每个之间添加填充来找出细节。我不是100%的填充物。但是,您可以通过检测标签宽度和 contentView 框架宽度之间的差异来计算填充。

对不起。我忘记了单元格的detailTextLabel 尺寸总是在不断变化;绝对不能依赖时期。您可以根据拥有的 UIViewController 的宽度和单元格textLabel 导出标签宽度。由于计算是在控制器级别进行的,因此您将知道 tableView 是否分组。如果是这样,您可以简单地将宽度缩小 0.8 倍左右,然后减去 textLabel 宽度。

同样适用于字体:self.textLabel.font.pointSizeself.detailTextLabel.font.pointSize

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-26
    • 2017-02-27
    • 1970-01-01
    • 1970-01-01
    • 2014-04-17
    • 1970-01-01
    • 2021-05-22
    • 1970-01-01
    相关资源
    最近更新 更多