iOS UITableViewCell高度自适应

一、高度自适应

高度自适应这个功能比较简单了,满足下面三点即可:

  • 1、设置UITableViewrowHeightUITableViewAutomaticDimension,如:

    _tableView.rowHeight = UITableViewAutomaticDimension;/// 高度自适应
    
  • 2、移除UITableViewDelegate的代理heightForRowAtIndexPath,就是下面的代码不要出现在页面内。

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    }
    
  • 3、设置好UITableViewCell内的控件约束。如:

    UILabel *label = [[UILabel alloc] init];
    [self.contentView addSubview:label];
    
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentView).offset(8);
        make.left.mas_equalTo(self.contentView).offset(12);
        make.right.mas_equalTo(self.contentView).offset(-12);
        make.height.mas_greaterThanOrEqualTo(20).priorityHigh();
        make.bottom.mas_equalTo(self.contentView).offset(-16);
    }];
    

二、遇到的问题 & 解决方法

1、约束警告

[LayoutConstraints] Unable to simultaneously satisfy constraints.

Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
(
****
)
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.

上面的问题是由于系统判定代码和编辑器中可能出现了重复约束,可以不做处理,跳过!解决办法就是给有问题的约束**设置优先级**

为什么会出现重复约束呢?如第一步中第三点,关于label的约束,正常情况下设置topleftbottomright已经可以确定一个控件的位置了,但是这里还设置了height的约束,这个height就是重复约束,给其设定优先级priorityHigh()即可。

2、UILabel内容展示不全

需求是完整展示多行文本。代码按常规方式写完,但实际测试结果却是在部分机型上,内容展示不全,显示...

解决方法:在设置完fontnumberOfLines之后,再设置adjustsFontSizeToFitWidth

但是在UITableViewUITableViewCell上的控件,设置完adjustsFontSizeToFitWidth之后,发现内容还是显示不全。这个时候需要额外设置preferredMaxLayoutWidth才能解决这个问题。

最终的效果就是:

UILabel *label = [[UILabel alloc] init];
label.font = PFFont_Regular(14);
label.numberOfLines = 0;
label.preferredMaxLayoutWidth = 200;
label.adjustsFontSizeToFitWidth = YES;
[self.contentView addSubview:label];

3、多个控件同时约束bottom

这里以简单情况为例:

效果一 效果二

如上面表格所示,由于UILabel内容的不确定性,UITableViewCell的高度需要根据多个控件来确定,可能是第一个控件,也可能是第二个控件。做法也比较简单给两个控件分别添加bottom约束。

/// 内容1
UILabel *label1 = [[UILabel alloc] init];
label1.font = PFFont_Regular(14);
label1.numberOfLines = 0;
[self.contentView addSubview:label1];

[label1 mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.mas_equalTo(self.contentView.mas_bottom).offset(15);
    make.left.mas_equalTo(self.contentView).offset(15);
    make.width.mas_equalTo(100);
    make.height.mas_greaterThanOrEqualTo(20).priorityHigh();
    make.bottom.mas_equalTo(self.contentView).offset(-15);
}];

/// 内容2
UILabel *label2 = [[UILabel alloc] init];
label2.font = PFFont_Regular(14);
label2.numberOfLines = 0;
[self.contentView addSubview:label2];

[label2 mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.mas_equalTo(self.contentView.mas_bottom).offset(15);
    make.right.mas_equalTo(self.contentView).offset(-15);
    make.width.mas_equalTo(100);
    make.height.mas_greaterThanOrEqualTo(20).priorityHigh();
    make.bottom.mas_equalTo(self.contentView).offset(-15);
}];

但是在实际情况下,上面情况中两个UILabel的高度其实是一致的,这种情况下我的解决办法是给Label文本居顶,具体做法参考文章

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容