【发布时间】:2016-08-24 16:12:04
【问题描述】:
我有一个自定义的 tableViewCell:
Extra text 标签有时会被隐藏,因此单元格应自动调整大小。
这个Extra text 标签是隐藏的,设置 .active = NO;对于标签周围的规则:
-(void)setShowExtraText:(BOOL)showExtraText
{
_showExtraText = showExtraText;
if (showExtraText) {
self.textfieldBottomSuperviewConstraint.active = NO;
self.extraText.hidden = NO;
self.textfieldExtraTextConstraint.active = YES;
self.leadingExtraTextConstraint.active = YES;
self.extraTextBottomContraint.active = YES;
}
else{
self.extraText.hidden = YES;
self.textfieldExtraTextConstraint.active = NO;
self.leadingExtraTextConstraint.active = NO;
self.textfieldBottomSuperviewConstraint.active = YES;
self.extraTextBottomContraint.active = NO;
}
}
只有一个单元格没有隐藏Extra text 标签,这是tableView 中的最后一个。
问题是当 tableView 加载单元格的高度是错误的。还看看它显示的Debug View Hierarchy 忽略了禁用规则的代码。因为已停用的规则仍在视图中。
如果我向下滚动并向上滚动,单元格的布局是正确的,正确显示带有额外文本的单元格和没有额外文本的单元格。
我尝试添加:
[self setNeedsLayout];
[self layoutIfNeeded];
我尝试使用 SizeClasses。 (我之前没有使用 sizeClasses )
我的想法用完了,有什么想法吗?
谢谢
更新 1
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellWithExtraText" forIndexPath:indexPath];
[cell setShowExtraText:(indexPath.section == LOGIN_SECTION && indexPath.row == REPEAT_PASSWORD_ROW)];
return cell;
}
更新 2
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellWithExtraText" forIndexPath:indexPath];
[cell setShowExtraText:(indexPath.section == LOGIN_SECTION && indexPath.row == REPEAT_PASSWORD_ROW)];
[cell updateConstraintsIfNeeded];
[self.tableView beginUpdates];
[self.tableView endUpdates];
[self.tableView setNeedsLayout];
[self.tableView layoutIfNeeded];
return cell;
}
【问题讨论】:
标签: ios uitableview autolayout