繁体   English   中英

Swift4 tableview 单元格高度不是动态的,始终是静态的

[英]Swift4 tableview cell height not dynamic and always static

请真的我需要关于这个问题的帮助,我花了 6 天时间,但我找不到解决方案。

我想制作简单的表格视图,但它的单元格始终具有静态高度,不会因其中的项目而改变

结果总是像emulator 的这个图像

我想在每个单元格中显示此示例:

1 1 11 1 1111 1 1 11 11 11 1 1 1 11 1 1 11 11 1 11 1 11 11 11

22 22 2 2 2 2 2 2 2 2 2 2 2 22 22 22 22 2 2 22 2 2 2 2 22 2 2 22

33 33 3 3 3 3 3 3 3 3 3 3 3 3 3 3 33 3 3 3 33 33 3 3 3 3 3 333 3 3 3 3

444 44 4 44 444 4 4 4 4 4 4 4 4 4 4 44 4 4 44 4 4 4 4 4 4 4 44 44 4

它必须以垂直对齐方式显示在 4 个标签中,但结果如您之前所见

我尝试了所有方法,例如使用故事板或代码制作视图,但最后,我使用了名为Stevia 的库,但结果仍然相同

这是视图控制器

    class AttendanceControlPage:UIViewController,UITableViewDataSource,UITableViewDelegate {

    @IBOutlet weak var dataTableView: UITableView!
    @IBOutlet weak var navLeftButton: UIBarButtonItem!


    //============================ Base Functions =================//
    override func viewDidLoad() {
        super.viewDidLoad()

        // Some tutorials said to put this two lines but not changes
        dataTableView.estimatedRowHeight = 44
        dataTableView.rowHeight = UITableViewAutomaticDimension


        initPage()

        dataTableView.register(AttendanceControlCell.self, forCellReuseIdentifier: "cellCon")

    }


    //============================ Init Functions =================//

    func initPage() {
            startLoading()
            self.navLeftButton.image = UIImage(named: "icn_drawer")
    }



    //==================== Nav Bar Buttons Actions ===================================//

    @IBAction func reloadPage(_ sender: Any) {
        self.dataArray.removeAll()
        loadData();
    }

    @IBAction func navLeftButtonAction(_ sender: Any) {
        showMenu()
    }

    //==================== Table view options ===================================//

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 6
    }

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


        let cell = dataTableView.dequeueReusableCell(withIdentifier: "cellCon", for: indexPath) as! AttendanceControlCell


        return cell
    }



}

这是细胞文件:

class AttendanceControlCell: UITableViewCell {

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)


        initFunc(cell : self)

    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!


    }
    func initFunc(cell : UITableViewCell)  {

        let name1 = UILabel()
        name1.numberOfLines = 0
        name1.text = "1 1 11  1 1111 1  1 11  11  11  1 1 1 11 1 1 11 11 1  11 1  11  11  11 "

        let name2 = UILabel()
        name2.numberOfLines = 0
        name2.text = "22 22 2 2 2 2 2 2 2 2 2 2 2  22  22  22 22 2 2  22 2 2 2 2  22 2  2 22 "

        let name3 = UILabel()
        name3.numberOfLines = 0
        name3.text = "33 33 3 3 3 3 3 3 3 3 3 3 3 3 3 3  33 3 3 3  33 33 3 3 3 3 3 333 3 3 3 3 "

        let name4 = UILabel()
        name4.numberOfLines = 0
        name4.text = "444 44 4 44 444 4 4 4 4 4 4 4 4 4 4  44 4 4  44 4 4 4  4 4 4 4 44  44  4 "

        cell.contentView.sv(
            name1,
            name2,
            name3,
            name4
        )
        layout(
            20,
            |name1|,
            20,
            |name2|,
            20,
            |name3|,
            20,
            |name4|
        )

    }

    override func awakeFromNib() {
        super.awakeFromNib()


    }

}

这是我的故事板:仅包含带有单元格 inisde 的 tableview

我的故事板图片

请我真的需要帮助

最后,我发现了问题。

问题是我在创建视图时忘记写底部最后一个视图的底部约束

所以这部分

layout(
        20,
        |name1|,
        20,
        |name2|,
        20,
        |name3|,
        20,
        |name4|
    )

应该用这个代替

layout(
        20,
        |name1|,
        20,
        |name2|,
        20,
        |name3|,
        20,
        |name4|,
        0
    )

感谢所有试图帮助我的人......我希望我的回答能帮助任何有 sam 问题的人

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM