日常收录

iOS tableViewCell嵌套UICollectionV

2022-07-27  本文已影响0人  aven_kang

这个自适应高度有三个前提

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        contentView.addSubview(collection)
        collection.snp.makeConstraints { make in
            make.left.equalToSuperview().inset(20)
            make.right.equalToSuperview().inset(20)
            make.top.equalToSuperview().inset(20)
            make.bottom.equalToSuperview().inset(20)
        }
    }
extension testCell {
    
    override func systemLayoutSizeFitting(_ targetSize: CGSize, withHorizontalFittingPriority horizontalFittingPriority: UILayoutPriority, verticalFittingPriority: UILayoutPriority) -> CGSize {
        
        let size = super.systemLayoutSizeFitting(targetSize, withHorizontalFittingPriority: horizontalFittingPriority, verticalFittingPriority: verticalFittingPriority)
        
        self.collection.layoutIfNeeded()
        let height = self.collection.collectionViewLayout.collectionViewContentSize.height
        return CGSize(width: size.width, height: size.height + height)
    }
}

完成以上三步,就可以实现collectionView在tableview中自适应高度啦

上一篇下一篇

猜你喜欢

热点阅读