【发布时间】:2016-04-28 13:48:38
【问题描述】:
我有 UITableView Cell,其中 4 个 UIView 像这样以垂直顺序排列,
在每个 UIView 中,我都有两个这样的标签,
我已经为所有四个 UIView 的高度约束采用了 IBOutlet,并且当该视图中的数据不可用时,我想将视图的高度设为 0。问题是由于该视图内 UILabel 的 10 px 底部约束,视图没有获得 0 高度。
我得到的是这样的,
处理约束的代码
NSString *toName = [self jointNameFromArray:evidence.arrTo];
if ([Utility isEmptyString:toName]) {
cell.toViewHeight.constant = 0;
}
else {
cell.lblToName.text = toName;
}
NSString *fromName = [self jointNameFromArray:evidence.arrFrom];
if ([Utility isEmptyString:fromName]) {
cell.fromViewHeight.constant = 0;
}
else {
cell.lblFromName.text = fromName;
}
NSString *ccName = [self jointNameFromArray:evidence.arrCc];
if ([Utility isEmptyString:ccName]) {
cell.ccViewHeight.constant = 0;
}
else {
cell.lblCCName.text = ccName;
}
NSString *custName = [self jointNameFromArray:evidence.arrCustodian];
if ([Utility isEmptyString:custName]) {
cell.custViewHeight.constant = 0;
}
else {
cell.lblCustName.text = custName;
}
[cell layoutIfNeeded];
【问题讨论】:
-
您也想隐藏“收件人”标签吗?
-
@TejaNandamuri 是的
-
当你改变一个约束时你需要调用 layoutIfNeeded 方法。
-
不是为两个标签提供两个高度约束,而是将 uiview 高度约束设置为 0 并在需要时调用布局。
-
@TejaNandamuri 是的,我已将 IBOutlet 用于 UIView 的高度,并在没有可用数据时将其更改为 0。我也尝试过 layoutIfNeeded 但没有运气。我在 cellForRow 方法中这样做。
标签: ios objective-c uitableview uiview autolayout