当前位置: 首页 > 知识库问答 >
问题:

单元格内的表视图动态高度问题

蔚学林
2023-03-14

我有一个具有动态高度的表视图与其他具有动态高度的UI组件一起在表视图中

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个答案

余天宇
2023-03-14

首先更改此代码:

self.addSubview(container)

contentView.addSubview(container)

单元格中的视图应添加到内容视图中,因为它是根布局。

 类似资料:
  • 场景- > TableView高度是使用纵横比相对于superview的高度动态设置的。 TableViewCell的高度是根据表格视图的高度计算的: 问题 - 最初在委托方法中没有正确计算表格视图高度。但在滚动单元格后正确计算。 尝试的解决方案: 在视图中重新加载。 调用 在单元格中进行. 有什么方法可以正确计算出这个表格的高度吗?

  • 我在UIViewController中有一个UITableView作为视图的一部分。我正在尝试根据文本的长度自动调整表格单元格的大小。它不会设置textview的文本,单元格将以idCellTextView单元格的标准高度结束。我已经四处搜索过,正在尝试从单元格内的文本视图相对于内容视图和代码在自动布局中使用固定,如下所示: 与问题相关的视图控制器的其余部分显示在以下位置:

  • 我有一个带有一堆单元格的表视图(自定义单元格,它只有它的内容视图)。 在 中,我正在向自定义单元格的content视图添加一个预定义的UIView(它有几个子视图)。我之前为UIView及其子视图设置了所有约束。 最后但并非最不重要的是,我为我的自定义单元格的内容视图(superview)和UIView设置了垂直和水平约束,UIView是在(subview)之前添加的。 约束字符串如下所示: 不幸

  • 我正在 中使用 现在我遇到了表视图单元格高度的问题。我正在聊天屏幕上工作,所以我有文字,图像。所以我在管理屏幕的其中取了一个原型单元。现在下面是我的身高代码 因此,文本单元格没有得到我所期望的,但我不知道我的错误在哪里,还有一件事,如果我< code > returnuitableviewautomaticdimension ,它的工作很好的文本信息,而不是图像。请帮助我,先谢了。

  • 我正在设计一个具有滚动视图的页面,其上方是表格视图(禁用滚动)。为此,我在这个问题中提到了答案 - 使UITableView不可滚动并调整高度以容纳所有单元格,但没有成功。 视图层次结构以及提供的约束- -主视图 -滚动视图< br >固定在主视图的所有边上(0,0,0,0),限制边距 -内容视图 固定到滚动视图(0,0,0,0),与主视图宽度相等,与主视图高度相等(优先级-250) -内容视图中

  • 我在表视图单元格下有一个集合视图。我在集合视图中显示所有标记值。现在,我想使表视图的高度动态变化,以便显示所有数据。用户不需要滚动集合视图。我在项目中使用自动布局。目前,我在tableView(_tableView:UITableView,heightForRowAt-indexPath:indexPath)方法中返回“UITableView.automaticDimension”,并使集合视图滚