I created a tableview with self-sizing UITableViewCells using
tableview.rowHeight = UITableViewAutomaticDimension;
For testing a created a single UIView and added it to cell.contentView with the following constraints. (using masonry)
- (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];
}
When i select a row i like to change the height of the selected cell. I do this by:
- (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];
}];
}
It seems to work but, xcode complaints with the following error:
Unable to simultaneously satisfy constraints.
Then problem is that contentView is creating a height constaint on its own (because contentView.translatesAutoresizingMaskIntoConstraints = YES; per default).
So when updateConstraints is first run, the system will create a height constraint on contentView equal to 10.0f
afterwards when setSelected... is called i alter the height of my testView so theres a conflict between testView.height (100.0f) and contentView.height (10.0f) and since testView is attached to the bottom of contentView (in order for self-sizing cells to work) it gives and error.
I tried setting contentView.translatesAutoresizingMaskIntoConstraints = NO; but then it seems the UITableViewCell cant really figure out a proper size for the cell. (sometimes it too wide / too small) etc.
What is the proper way to implement self-sizing cells which can have their height changed dynamicly?
estimatedRowHeightproperty? - Michael