单元格内的TableView动态高度问题

时间:2018-09-26 08:25:11

标签: ios swift uitableview

我在tableViewCell中有一个具有动态高度的tableView以及其他具有动态高度和单元格比例宽度的ui组件。问题:渲染和滚动外部uitableview时,内部tableViewCells缩小。这是代码:

import UIKit
import WebKit
import SVProgressHUD

class DashboardProgramcell: UITableViewCell, UITableViewDataSource, UITableViewDelegate{

数据,此处为模型。

    public var icerikModel:WMPIcerikModel?
    static let ID:String = "DashboardProgramcell"
    static let turCellPair = (tur: TurID.Program, cellId: ID)


    var kListData: WMPArrayOfKatilimciModel?

单元格子视图。

    //MainContainer
    var container: UIView = {
        var container = UIView()
        container.translatesAutoresizingMaskIntoConstraints = false
        return container
    }()


    //Left side
    var leftContainer: UIView = {
        var leftContainer = UIView()
        leftContainer.translatesAutoresizingMaskIntoConstraints = false
        return leftContainer
    }()

    var gSaat: UILabel = {
        var gSaat = UILabel()
        gSaat.translatesAutoresizingMaskIntoConstraints = false
        gSaat.numberOfLines = 0
        gSaat.font = UIFont.systemFont(ofSize: 11)
        return gSaat
    }()

    var sure: UILabel = {
        var sure = UILabel()
        sure.translatesAutoresizingMaskIntoConstraints = false
        sure.numberOfLines = 0
        sure.backgroundColor = ColorHelper.mercury
        sure.textAlignment = .center
        sure.font = UIFont.systemFont(ofSize: 11)
        return sure
    }()


    var ptad: UITextView = {
        var ptad = UITextView()
        ptad.textColor = .white
        ptad.textAlignment = .center
        ptad.isUserInteractionEnabled = false
        ptad.isScrollEnabled = false
        ptad.translatesAutoresizingMaskIntoConstraints = false
        ptad.font = UIFont.systemFont(ofSize: 11)
        ptad.textContainerInset = UIEdgeInsets.zero
        ptad.textContainer.lineFragmentPadding = 0
        return ptad
    }()



    //Right part
    var rightContainer: UIView = {
        var rightContainer = UIView()
        rightContainer.translatesAutoresizingMaskIntoConstraints = false
        return rightContainer
    }() 


    var rightTopTextView: UILabel = {
        var rightTopTextView = UILabel()
        rightTopTextView.translatesAutoresizingMaskIntoConstraints = false
        rightTopTextView.numberOfLines = 0
        rightTopTextView.lineBreakMode = NSLineBreakMode.byWordWrapping
        rightTopTextView.font = UIFont.systemFont(ofSize: 12)
        rightTopTextView.backgroundColor = ColorHelper.arzPopup
        return rightTopTextView
    }() 

这是内部tableView及其约束!

    var kList: UITableView = {
        var kList = UITableView()
        kList.contentMode = .scaleAspectFit
        kList.bounces = false
        kList.bouncesZoom = false
        kList.alwaysBounceVertical = false
        kList.translatesAutoresizingMaskIntoConstraints = false
        kList.tag = KListCell.TAG
        kList.rowHeight = UITableView.automaticDimension
        kList.estimatedRowHeight = UITableView.automaticDimension
        kList.register(KListCell.self, forCellReuseIdentifier: KListCell.ID)
        kList.tableFooterView = UIView(frame: .zero)
        return kList
    }()

    var kListHeightConstraint:NSLayoutConstraint?
    var rTopTextViewHeightConstraint:NSLayoutConstraint?

构造函数和约束激活!

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.tag = TurID.Program.rawValue
        self.selectionStyle = .none 
        self.addSubview(container)

        container.addSubview(leftContainer) //OK
        container.addSubview(rightContainer) //OK
        leftContainer.addSubview(gSaat) //OK
        leftContainer.addSubview(sure) //OK
        leftContainer.addSubview(ptad)
        rightContainer.addSubview(rightTopTextView)
        rightContainer.addSubview(kList)

        NSLayoutConstraint.activate([

            container.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 2),
            container.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -2),
            container.topAnchor.constraint(equalTo: self.topAnchor, constant: 2),
            container.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: -2),

            leftContainer.topAnchor.constraint(equalTo: container.topAnchor),
            leftContainer.leftAnchor.constraint(equalTo: container.leftAnchor),
            leftContainer.bottomAnchor.constraint(equalTo: container.bottomAnchor),
            leftContainer.widthAnchor.constraint(equalTo: container.widthAnchor, multiplier: 0.30),
            leftContainer.heightAnchor.constraint(equalTo: container.heightAnchor),
            leftContainer.rightAnchor.constraint(equalTo: rightContainer.leftAnchor),

            rightContainer.topAnchor.constraint(equalTo: container.topAnchor),
            rightContainer.bottomAnchor.constraint(equalTo: container.bottomAnchor),
            rightContainer.widthAnchor.constraint(equalTo: container.widthAnchor, multiplier: 0.68),
            rightContainer.heightAnchor.constraint(equalTo: container.heightAnchor),
            rightContainer.rightAnchor.constraint(equalTo: container.rightAnchor),

            gSaat.topAnchor.constraint(equalTo: leftContainer.topAnchor, constant: 4),
            gSaat.leftAnchor.constraint(equalTo: leftContainer.leftAnchor, constant: 4),
            gSaat.rightAnchor.constraint(equalTo: leftContainer.rightAnchor, constant: -4),
            gSaat.bottomAnchor.constraint(equalTo: sure.topAnchor, constant: -2),

            sure.leftAnchor.constraint(equalTo: leftContainer.leftAnchor, constant: 4),
            sure.rightAnchor.constraint(equalTo: leftContainer.rightAnchor, constant: -4),
            sure.bottomAnchor.constraint(equalTo: ptad.topAnchor, constant:-2),

            ptad.leftAnchor.constraint(equalTo: leftContainer.leftAnchor, constant: 4),
            ptad.rightAnchor.constraint(equalTo: leftContainer.rightAnchor),
            ptad.bottomAnchor.constraint(lessThanOrEqualTo: leftContainer.bottomAnchor),
            ptad.heightAnchor.constraint(equalToConstant: 16),

            rightTopTextView.topAnchor.constraint(equalTo: rightContainer.topAnchor, constant:4),
            rightTopTextView.leftAnchor.constraint(equalTo: rightContainer.leftAnchor, constant:4),
            rightTopTextView.rightAnchor.constraint(equalTo: rightContainer.rightAnchor, constant:-4),
            rightTopTextView.bottomAnchor.constraint(equalTo: kList.topAnchor),

            kList.leftAnchor.constraint(equalTo: rightContainer.leftAnchor, constant:2),
            kList.rightAnchor.constraint(equalTo: rightContainer.rightAnchor, constant:-2),
            kList.bottomAnchor.constraint(equalTo: rightContainer.bottomAnchor, constant:-2)

        ])

        container.addBorders(edges: [.left, .right, .bottom], color: ColorHelper.programBg, width: 1)

        kList.dataSource = self
        kList.delegate = self

//        kListHeightConstraint = kList.heightAnchor.constraint(equalToConstant: 150)
//        kListHeightConstraint?.isActive = true

//        rTopTextViewHeightConstraint = rightTopTextView.heightAnchor.constraint(equalToConstant: 120)
//        rTopTextViewHeightConstraint?.isActive = true

        self.rightTopTextView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(tapItself)))
        self.layoutIfNeeded()
    }

    @objc func tapItself()
    {
        SVProgressHUD.show()
        DispatchQueue.global(qos: .background).async {
            SoapService(vc: self.viewControllerForTableView!).KayitDetay(tur: self.icerikModel!.TID!, kayitID: self.icerikModel!.ID!)
        }
    }



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

    public func adjustSubViews()
    {
         DispatchQueue.main.async {

            if let icerik = self.icerikModel {

                //Left
                self.gSaat.text = icerik.GSaat ?? ""
                self.sure.text = icerik.Sure ?? ""
                self.ptad.text = icerik.PTAD ?? ""
                self.ptad.backgroundColor = self.ptad.text.isEmpty ? UIColor.clear : (self.ptad.text.hasPrefix("Arz") || self.ptad.text.hasPrefix("arz") ? TurID.Arz.tableviewBg() : TurID.Ziyaret.tableviewBg())

                //Right
                if let ack = icerik.Ack
                {
                    self.rightTopTextView.attributedText = ack.convertHTML().convertHtmlAttributed()
//                    self.rTopTextViewHeightConstraint?.constant = self.rightTopTextView.contentSize.height
                }
                else
                {
                    self.rightTopTextView.heightAnchor.constraint(equalToConstant: 0).isActive = true
                }

                if let KListe = icerik.KListe, KListe.count != 0 {
                    self.kListData = KListe
                    self.kList.reloadData()
                }
                else
                {
//                    self.kListHeightConstraint?.constant = 5
                    SVProgressHUD.dismissMainUI { }
                }

                self.layoutIfNeeded()
            }
        }
    }


    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if let _kListData = self.kListData, _kListData.count != 0 {
            return _kListData.count
        }
        return 0
    }

    var kTVHeight:CGFloat = 0

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let kListCell = tableView.dequeueReusableCell(withIdentifier: KListCell.ID)  as!  KListCell
        if let _kListData = self.kListData , _kListData.count != 0 {
            kListCell.katilimciModel = _kListData[indexPath.row]
            kListCell.adjustSubViews()
//            self.kTVHeight += kListCell.frame.height
            if indexPath.row == _kListData.count - 1 && kTVHeight > 10
            {
//                kListHeightConstraint?.constant = kTVHeight
//                rTopTextViewHeightConstraint?.constant = rightTopTextView.contentSize.height
//                kTVHeight = 0
            }
        }
        return kListCell
    }


    //If we got cell to display this func will work!
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {

        if tableView.visibleCells.count == 0 {
            SVProgressHUD.dismissMainUI {  }
        }
    }




}//class DashboardProgramcell

class  KListCell: UITableViewCell {

    var katilimciModel:WMPKatilimciModel?
    static let ID:String = "KListCell"
    static let TAG = 4001

    //MainContainer
    var container: UIView = {
        var container = UIView()
        container.translatesAutoresizingMaskIntoConstraints = false
        return container
    }()


    //Left side
    var leftContainer: UIView = {
        var leftContainer = UIView()
        leftContainer.translatesAutoresizingMaskIntoConstraints = false
        return leftContainer
    }()

    //Right part
    var rightContainer: UIView = {
        var rightContainer = UIView()
        rightContainer.translatesAutoresizingMaskIntoConstraints = false
        return rightContainer
    }()

    var adSoyad: UILabel = {
        var adSoyad = UILabel()
        adSoyad.translatesAutoresizingMaskIntoConstraints = false
        adSoyad.numberOfLines = 0
        adSoyad.font = UIFont.systemFont(ofSize: 10)
        return adSoyad
    }()

    var unvan: UILabel = {
        var unvan = UILabel()
        unvan.translatesAutoresizingMaskIntoConstraints = false
        unvan.numberOfLines = 0
        unvan.font = UIFont.systemFont(ofSize: 10)
        return unvan
    }()

    var userImage: UIImageView = {
        var userImage = UIImageView(frame: CGRect(x: 0, y: 0, width: 12, height: 12))
        userImage.translatesAutoresizingMaskIntoConstraints = false
        return userImage
    }()

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.addSubview(container)
        container.addSubview(leftContainer) //OK
        container.addSubview(rightContainer) //OK
        leftContainer.addSubview(userImage) //OK
        rightContainer.addSubview(adSoyad)
        rightContainer.addSubview(unvan)

        self.applyConstraints()
    }


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

    func applyConstraints()
    {
        NSLayoutConstraint.activate([

            container.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 2),
            container.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -2),
            container.topAnchor.constraint(equalTo: self.topAnchor, constant: 2),
            container.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: -2),

            leftContainer.topAnchor.constraint(equalTo: container.topAnchor),
            leftContainer.leftAnchor.constraint(equalTo: container.leftAnchor),
            leftContainer.bottomAnchor.constraint(equalTo: container.bottomAnchor),
            leftContainer.widthAnchor.constraint(equalTo: container.widthAnchor, multiplier: 0.12),
            leftContainer.heightAnchor.constraint(equalTo: container.heightAnchor),
            leftContainer.rightAnchor.constraint(equalTo: rightContainer.leftAnchor),

            rightContainer.topAnchor.constraint(equalTo: container.topAnchor),
            rightContainer.bottomAnchor.constraint(equalTo: container.bottomAnchor),
            rightContainer.widthAnchor.constraint(equalTo: container.widthAnchor, multiplier: 0.86),
            rightContainer.heightAnchor.constraint(equalTo: container.heightAnchor),
            rightContainer.rightAnchor.constraint(equalTo: container.rightAnchor),

            userImage.topAnchor.constraint(equalTo: leftContainer.topAnchor, constant:2),
            userImage.heightAnchor.constraint(equalToConstant: 12),
            userImage.widthAnchor.constraint(equalToConstant: 12),
            userImage.centerXAnchor.constraint(equalTo: leftContainer.centerXAnchor),
            userImage.bottomAnchor.constraint(lessThanOrEqualTo: leftContainer.bottomAnchor),

            adSoyad.topAnchor.constraint(equalTo: rightContainer.topAnchor, constant: 2),
            adSoyad.leftAnchor.constraint(equalTo: rightContainer.leftAnchor, constant: 2),
            adSoyad.rightAnchor.constraint(equalTo: rightContainer.rightAnchor, constant: -2),
            adSoyad.bottomAnchor.constraint(equalTo: unvan.topAnchor),

            unvan.leftAnchor.constraint(equalTo: rightContainer.leftAnchor, constant: 2),
            unvan.rightAnchor.constraint(equalTo: rightContainer.rightAnchor, constant: -2),
            unvan.bottomAnchor.constraint(equalTo: rightContainer.bottomAnchor, constant: -2),

        ])
    }


    public func adjustSubViews ()
    {
        DispatchQueue.main.async {
            if let katilimci = self.katilimciModel {
                self.adSoyad.text = katilimci.AD ?? ""
                self.unvan.text = katilimci.Ack ?? ""
                self.userImage.image = UIImage(named:"userBlack")
            }
        }
    }



}

1 个答案:

答案 0 :(得分:1)

首先更改此代码:

self.addSubview(container)

contentView.addSubview(container)

由于单元格中的视图是根布局,因此应将其添加到contentView中。