1.cell自动布局应用场景
对于一个cell有多种状态,涉及到显示或者隐藏某些view,并且cell高度随时改变,如果用viewmodel来计算的话,很复杂增加了很多代码量,而且如果状态考虑的不全极有可能造成错误,如果用自动布局的话,则没有这么多的顾及。
2.tableView设置
- tableview的懒加载中设置这两个属性
_tableView.estimatedRowHeight = 110*kWidthRatio;
_tableView.rowHeight = UITableViewAutomaticDimension;
UITableViewAutomaticDimension 是float型返回的值恒为-1,意思是告诉tableview我的cell是自动布局的。这句代码等同于_tableView.rowHeight = -1;
- 如果你的tableview中有多种cell有的cell布局不会改变,那么久可以用绝对布局,这时不要设置_tableView.rowHeight,而是在代理中实现
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
switch (indexPath.section) {
case 0:{
return UITableViewAutomaticDimension;
}
break;
case 1:{
return 110*kWidthRatio;
}
break;
default:
return 0;
break;
}
}
这里代码可以看出tableview中既有自动布局又有绝对布局
3.自定义Cell内的布局
+ (BOOL)requiresConstraintBasedLayout
{
return YES;
}
- (void)updateConstraints{
WEAKSELF
//物流
[self.wuLiuView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(0);
make.top.mas_equalTo(0);
make.width.mas_equalTo(ScreenW).priority(750);
//切换物流信息展示状态, 同一个约束更改不同的状态设置不同的优先级以避免约束冲突
if (weakSelf.wuLiuView.isHidden) {
make.bottom.mas_equalTo(0).priority(500);
}else{
make.bottom.equalTo(self.dateLabel.mas_bottom).offset(15