如何在不使用估算高度计算的情况下使uicollectionviewcell高度动态化

时间:2019-07-20 05:06:30

标签: ios uicollectionview uicollectionviewcell swift4.2

我想将UICollectionViewCell设置为动态高度,而不先使用估算的高度进行计算。

我已经尝试过堆栈溢出和其他一些网站的答案,但最后没有找到与我的查询完全匹配的内容。

我的 FireStoreChatViewController 类:

import UIKit
import Kingfisher

class FireStoreChatViewController: SuperViewController, UITextViewDelegate,UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout
{
    var contentHeight: CGFloat = 0.0

    @IBOutlet var collectionView: UICollectionView!

    override func viewDidLoad()
    {
        super.viewDidLoad()
    }

   func collectionView(_ collectionView: UICollectionView,           numberOfItemsInSection section: Int) -> Int
    {
        return 2
    }

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
    {
        if indexPath.row%2 == 0{
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Message", for: indexPath as IndexPath) as! MessageCollectionViewCell

            cell.layer.cornerRadius = 8.0
            cell.layer.masksToBounds = true

            contentHeight = CGFloat(cell.senderUIView2.frame.size.height + cell.senderUIView3.frame.size.height + cell.senderImage.frame.size.height + cell.senderText.frame.size.height
                + 30);

            return cell
        }
        else
        {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Message User", for: indexPath as IndexPath) as! MessageUserCollectionViewCell

            cell.layer.cornerRadius = 8.0
            cell.layer.masksToBounds = true

            contentHeight = CGFloat(cell.reciverUIView2.frame.size.height + cell.reciverUIView3.frame.size.height + cell.reciverImage.frame.size.height + cell.reciverText.frame.size.height
                + 30);

            return cell
        }

    }

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
    {


        return CGSize(width: view.frame.width, height: contentHeight )

    }
}

我的 UICollectionViewCell 类:

import UIKit

class MessageCollectionViewCell: UICollectionViewCell
{

    @IBOutlet weak var senderUIView1: UIView!
    @IBOutlet weak var senderStackView: UIStackView!

    @IBOutlet weak var senderUIView2: UIView!

    @IBOutlet weak var senderUIView3: UIView!
    @IBOutlet weak var senderDate: UILabel!


    @IBOutlet weak var senderUIView4: UIView!
    @IBOutlet weak var senderUIView5: UIView!

    @IBOutlet weak var senderTime: UILabel!

    @IBOutlet weak var senderContentStackView: UIStackView!
    @IBOutlet weak var senderImage: UIImageView!
    @IBOutlet weak var senderText: UILabel!


    override func awakeFromNib()
    {
        senderDate.layer.cornerRadius = 10.0
        senderDate.layer.masksToBounds = true
        senderUIView5.layer.cornerRadius = 8.0
    }

}

高度仅应按0.0计算为

 var contentHeight: CGFloat = 0.0

预先感谢

0 个答案:

没有答案