UITableViewCellStyleValue2 样式的单元格上的detailTextLabel 的宽度

发布于 2024-09-25 22:39:27 字数 795 浏览 7 评论 0原文

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

我正在尝试使用带有多行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的宽度约为167px,但想确切地知道它并且不喜欢它硬编码(考虑方向变化)。

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

感谢您的帮助!

I'm new to stack overflow and to cocoa-touch developing.

I'm trying to build a UITableViewCell using the UITableViewCellSytleValue2 with a multi-line detailTextLabel.

Therefore I implemented the tableView:heightForRowAtIndexPath: like that:

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

I figured out the detailTextLabel's width is approximately 167px, but want to know it exactly and don't like it hardcoded (considering orientation changes).

I tried to access the detailTextLabel's frame or bounds, but it returns 0.0 for it's width.

Thanks for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

生活了然无味 2024-10-02 22:39:27

创建自定义单元格

Create a custom Cell

节枝 2024-10-02 22:39:27

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

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

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

对于字体也是如此:self.textLabel.font.pointSizeself.detailTextLabel.font.pointSize

You could define a static set of variables in a custom implementation of UITableViewCell that sets these sorts of parameters in layoutSubviews if and only if not set. Be sure to call [super layoutSubviews] first.

This way you inherit the device defaults, and you never have to wonder.
AFAIK, the cell width is self.contentView.frame.size.width. You can use self.textLabel.frame.size.width and self.detailTextLabel.frame.size.width, and self.accessoryView.frame.size.width + padding between each to figure out the details. I'm not 100% on the padding. But, you could calculate the padding by detecting the difference between the label widths and contentView frame width.

Sorry. I forgot the cell's detailTextLabel dimensions are always in flux; and definitely not to be relied on period. You can derive the label width based on the width of the owning UIViewController, and the cells textLabel. Since the calculation is at the controller level, you'll know whether the tableView is grouped. And if so, you can simply scale the width back by a factor of 0.8 or so, and subtract the textLabel width.

Likewise for fonts: self.textLabel.font.pointSize, and self.detailTextLabel.font.pointSize.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文