我创建使用iOS 8的自上浆细胞与改变高度
tableview.rowHeight = UITableViewAutomaticDimension;
具有自上浆UITableViewCells一个的tableview用于测试的创建单个UIView
并将其与下面的约束添加至cell.contentView
。 (使用砖石)
- (void)updateConstraints {
UIView *superview = self.contentView;
if (!_didSetupConstraints) {
[self.testView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(superview);
make.height.equalTo(@10.0f);
make.left.equalTo(superview);
make.right.equalTo(superview);
make.bottom.equalTo(superview);
}];
_didSetupConstraints = YES;
}
[super updateConstraints];
}
当我选择一行我喜欢改变所选单元格的高度。
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
[self.testView mas_updateConstraints:^(MASConstraintMaker *make) {
if (selected) {
make.height.equalTo(@100);
}
else {
make.height.equalTo(@10);
}
}];
[UIView animateWithDuration:0.3f animations:^{
[super layoutIfNeeded];
}];
}
看来工作,但是,并出现以下错误的Xcode投诉:
Unable to simultaneously satisfy constraints.
那么问题是contentView
创造本身就是一个高度constaint(因为contentView.translatesAutoresizingMaskIntoConstraints = YES;
我通过这样做每个默认值)。
所以当updateConstraints是第一次运行时,系统将在contentView
等于10.0f
后来当setSelected...
叫我改变我的testView
的高度,创造一个高度约束所以那里有testView.height
(100.0f)之间的冲突contentView.height
(10.0f)并且由于testView
附加到contentView
的底部(为了使自定尺寸的单元工作),它给出了错误。
我试过设置contentView.translatesAutoresizingMaskIntoConstraints = NO;
,但它似乎UITableViewCell无法真正找出一个适当的单元格大小。 (有时它太宽/太小)等。
什么是适当的方式来实现自动调整大小的细胞,它们的高度可以动态变化?
你有没有设置'estimatedRowHeight'财产? – Michael
我没有设置estimatedRowHeight :)但只是测试,并没有对高度限制冲突的问题有任何影响。 –