forkingdog / UITableView-FDTemplateLayoutCell

Template auto layout cell for automatically UITableViewCell height calculating
MIT License
9.94k stars 2.01k forks source link

发现一种情况会出现死循环 #288

Closed xiaobing1024 closed 7 years ago

xiaobing1024 commented 7 years ago

cell的布局:

[self.contentView addSubview:self.lab1];
[_lab1 mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.top.equalTo(@10);
    make.bottom.equalTo(@-10);
 }];

[self.contentView addSubview:self.lab2];
[_lab2 mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(_lab1.mas_right).offset(5);
    make.right.lessThanOrEqualTo(@10);
    make.centerY.equalTo(_lab1);
}];

两个lab的设置:

- (UILabel *)lab1 {
    if (!_lab1) {
        _lab1=[[UILabel alloc] init];
        _lab1.backgroundColor=[UIColor redColor];
        _lab1.numberOfLines=0;
        _lab1.lineBreakMode=NSLineBreakByCharWrapping;
    }
    return _lab1;
}

- (UILabel *)lab2 {
    if (!_lab2) {
        _lab2=[[UILabel alloc] init];
        _lab2.backgroundColor=[UIColor greenColor];
        [_lab2 setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
        [_lab2 setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    }
    return _lab2;
}

当lab1长度达到换行时,就会死循环:

_lab1.text=@"标题标题标题标题标题标题标题标题标题标题标题标题";
_lab2.text=@"标签";

我自己计算是没有问题的:

[cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height+1.0;
xiaobing1024 commented 7 years ago

是我自己写的有问题:

_lab1.preferredMaxLayoutWidth=SCREEN_WIDTH-20;

加上就没问题了