UITableViewCellStyleValue2 样式的单元格上的detailTextLabel 的宽度
我是堆栈溢出和可可触摸开发的新手。
我正在尝试使用带有多行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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建自定义单元格
Create a custom Cell
您可以在 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.pointSize
和self.detailTextLabel.font.pointSize
。You could define a static set of variables in a custom implementation of UITableViewCell that sets these sorts of parameters inlayoutSubviews
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 useself.textLabel.frame.size.width
andself.detailTextLabel.frame.size.width
, andself.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 cellstextLabel
. 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 thetextLabel
width.Likewise for fonts:
self.textLabel.font.pointSize
, andself.detailTextLabel.font.pointSize
.