【发布时间】:2016-05-12 15:17:00
【问题描述】:
我有一个自定义的UITableViewCell。在 UITableViewCell 的contentView 中,我添加了一个UIButton,在按钮下方有一个UILabel(按钮和UILabel 是彼此的兄弟)。
一开始 UILabel 上没有文本,因此它的高度限制设置为 >= 0。但是当我点击按钮时,我设置了 UILabel 的文本属性,它应该变得可见。换句话说,我想扩大 UILabel 的高度,也应该扩大 UITableViewCell 的高度。
我已将 UIButton Top,Leading 和 Trailing 边缘固定到 UITableViewCell contentView,同样我已将 UILabel 的 Bottom,Leading 和 Trailing 边缘固定到 UITableViewCell contentsView。
现在的问题是,当我点击UITableViewCell 时,它不会调整UILabel 的大小,也不会调整UITableViewCell。
这是我的代码
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
MYCustomCell *cell = (MYCustomCell *) [tableView cellForRowAtIndexPath:indexPath];
cell.descriptionLbl.text = @"This is my text";
[cell layoutSubviews];
}
同样
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
MYCustomCell *cell = (MYCustomCell *) [tableView cellForRowAtIndexPath:indexPath];
cell.descriptionLbl.text = @"";
[cell layoutSubviews];
}
这是我的自定义单元格的 layoutSubViews
- (void) layoutSubviews {
[super layoutSubviews];
self.descriptionLbl.preferredMaxLayoutWidth = CGRectGetWidth(self.contentView.bounds);
self.descriptionLbl.numberOfLines = 0;
[self.descriptionLbl sizeToFit];
if (self.descriptionLbl.text.length > 0){
self.descriptionLbl.frame = CGRectMake(self.descriptionLbl.frame.origin.x, self.descriptionLbl.frame.origin.y, self.descriptionLbl.frame.size.width, 50); // Here the size will be calculated more accurately later
}
}
请注意,我没有在任何地方设置UITableViewCell 的高度。
【问题讨论】:
标签: ios uitableview autolayout uilabel