美文网首页
iOS UITableView Cell高度自适应方案

iOS UITableView Cell高度自适应方案

作者: 芮淼一线 | 来源:发表于2021-12-04 14:58 被阅读0次

记录一下

方案一

self.tableView.estimatedRowHeight = 0;

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return  UITableViewAutomaticDimension;
}

方案二

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return  UITableViewAutomaticDimension;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
}

方案三

直接使用rowHeightestimatedRowHeight属性实现Cell高度自适应。

方式1:

self.tableView.estimatedRowHeight = 40;//期望值必须大于1
self.tableView.rowHeight = UITableViewAutomaticDimension;

方式2:
也可以直接使用单个属性

self.tableView.rowHeight = UITableViewAutomaticDimension;

或者:

self.tableView.estimatedRowHeight = 40;//期望值必须大于1

方式3:
甚至可以不设置与高度相关的属性与代理,正确设置cell的约束,即可实现高度自适应,不过不推荐

注意:

这几种方案都能实现cell高度自适应功能,但是它们与cell中设置的约束是有关的,如果设置不正确可能会导致莫名奇妙的问题,所以:
推荐使用至少设置2个属性(或者代理)的方案来实现高度自适应功能

相关文章

网友评论

      本文标题:iOS UITableView Cell高度自适应方案

      本文链接:https://www.haomeiwen.com/subject/zqnvxrtx.html