将多行添加到UITableViewCell中的detailTextLabel [英] Add multiple lines to detailTextLabel in UITableViewCell
问题描述
我正在使用UITableView
,其单元格样式为UITableViewCellStyleValue1
.
I'm using a UITableView
with cell style of UITableViewCellStyleValue1
.
我想在detailTextLabel
中包含多行,这可能吗?还是我必须自定义单元格?
I'm want to have multiple lines in the detailTextLabel
, is that possible? Or do I have to make a custom cell?
推荐答案
您可以设置cell.detailTextLabel.numberOfLines = 2
在其中获得2行.但是,我怀疑在这种情况下,UITableViewCell是否会按您期望的方式布置标签.您可能想要子类化UITableViewCell并覆盖-layoutSubviews
来按需要放置标签.您可以调用[super layoutSubviews]
,然后仅调整标签的位置.您可能需要使用-[NSString sizeWithFont:constrainedToSize:lineBreakMode:]
来计算详细信息文本标签的正确大小.
You can set cell.detailTextLabel.numberOfLines = 2
to get 2 lines in there. However, I doubt UITableViewCell will lay out the labels as you expect in that case. You may want to subclass UITableViewCell and override -layoutSubviews
to position the labels how you want. You can call [super layoutSubviews]
and then just tweak the positions of the labels. You'll probably want to use -[NSString sizeWithFont:constrainedToSize:lineBreakMode:]
to calculate the correct size for the detail text label.
或者,您可以尝试在-tableView:willDisplayCell:forRowAtIndexPath:
中进行调整,而不是对UITableViewCell进行子类化,但是如果该单元格决定需要对其进行重新布局,则您的调整将被清除.我建议您使用子类化方法.
Alternatively, instead of subclassing UITableViewCell, you could try doing the tweaks in -tableView:willDisplayCell:forRowAtIndexPath:
, though if the cell ever decides it needs to re-layout, then your tweaks will be erased. I recommend you go with the subclassing approach.
编辑:顺便说一句,使用子类方法,您要做的就是将[UITableViewCell alloc]
更改为[MyTableViewCellSubclass alloc]
.由于您没有引入新的方法或属性,因此该变量仍可以输入为UITableViewCell,而无需更改任何其他代码.
Edit: BTW, with the subclass approach, all you have to do is change [UITableViewCell alloc]
to [MyTableViewCellSubclass alloc]
. Since you're not introducing new methods or properties, the variable can still remain typed as a UITableViewCell and you won't have to change any other code.
这篇关于将多行添加到UITableViewCell中的detailTextLabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!