ios-tableView自动计算行高

最近通过学习了解到一个东西就是tableView可以自动的计算行高了,只需要配合autoLayout来使用就可以了

先设置如下所示的代码

 //先设置预估行高
       tableView.estimatedRowHeight = 200
      //再设置自动计算行高
      tableView.rowHeight = UITableViewAutomaticDimension

然后你要保证你在cell里面的控件,自上而下是有约束条件的最后一个控件要么有高度的限制要么有底部的约束限制,这样自上而下才可能计算成功。也就是说设置约束的时候必须注意每个控件在垂直方向上必须都有约束。

比如说这样子,我们设置了高度的约束,顶部的约束,然后最下面那个控件我设置了顶部的约束和底部的约束

  //2、自动布局,添加顶部的约束
       contentView.addConstraint(NSLayoutConstraint(item: statusCellTopView, attribute:.top, relatedBy: .equal, toItem: contentView, attribute: .top, multiplier: 1, constant: 0))
       //添加左边的约束
       contentView.addConstraint(NSLayoutConstraint(item: statusCellTopView, attribute: .left, relatedBy: .equal, toItem: contentView, attribute: .left, multiplier: 1, constant: 0))
       //添加右边的约束
       contentView.addConstraint(NSLayoutConstraint(item: statusCellTopView, attribute: .right, relatedBy: .equal, toItem: contentView, attribute: .right, multiplier: 1, constant: 0))
       //添加高度的约束
        contentView.addConstraint(NSLayoutConstraint(item: statusCellTopView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant:CGFloat(WBCellMargin+WBCellIconViewWH)))
        
       //添加正文的顶部约束
        contentView.addConstraint(NSLayoutConstraint(item: ContentText, attribute: .top, relatedBy: .equal, toItem:statusCellTopView, attribute: .bottom, multiplier: 1, constant: CGFloat(WBCellMargin)))
       //添加正文的左边的约束
        contentView.addConstraint(NSLayoutConstraint(item: ContentText, attribute: .left, relatedBy: .equal, toItem: contentView, attribute: .left, multiplier: 1, constant: CGFloat(WBCellMargin)))
        //设置底部的约束
        contentView.addConstraint(NSLayoutConstraint(item: ContentText, attribute: .bottom, relatedBy: .equal, toItem: contentView, attribute: .bottom, multiplier: 1, constant: CGFloat(-WBCellMargin)))







在 UITableView 中,设置预估行高(estimatedRowHeight)可以提高滚动性能,特别是在有大量行的情况下。通过设置预估行高,UITableView 可以在首次加载和滚动时快速估算出整个内容的高度,从而更好地管理内存和渲染。 要设置预估行高,您可以按照以下步骤进行操作: 1. 在 UITableViewController 或 UITableViewDelegate 中的 viewDidLoad 方法中,添加以下代码: ```objective-c self.tableView.estimatedRowHeight = 100; // 设置预估行高 ``` 这将设置 UITableView 的预估行高为 100,您可以根据实际需求进行调整。 2. 确保 UITableViewDelegate 的 heightForRowAtIndexPath 方法实现返回正确的行高。UITableView计算实际行高时会使用这个方法。 ```objective-c - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { // 返回实际行高的代码逻辑 return 50; // 返回实际行高值 } ``` 在 heightForRowAtIndexPath 方法中,您需要根据自己的数据和布局来计算并返回每一行的实际行高。 通过设置预估行高,UITableView 将使用预估值来计算整个内容的高度,从而提高滚动性能。当实际行高和预估行高差距较大时,UITableView 会进行额外的调整,确保滚动时的平滑性和正确的显示。 请注意,在使用预估行高时,如果您的 UITableView 中有可变高度的行,您可能还需要实现 UITableViewDelegate 的 estimatedHeightForRowAtIndexPath 方法,返回预估的行高值。 这样设置后,您的 UITableView 将使用预估行高来提高滚动性能。如有其他问题,请随时提问!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值