Swift TableView Cell动态高度

作者: 屈涯 | 来源:发表于2019-04-30 13:40 被阅读0次

首先需要竖向约束不间断, 并设置

        tableView.estimatedRowHeight = 0

这样 UITableView.automaticDimension 作为返回高度就会生效

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.row == 0 {
            return 100.0
        } else if indexPath.row == 2 {
            return 75.0
        } else if indexPath.row == 1 {
            return 200.0
        } else if indexPath.row == 3 {
            return 360.0
        } else if indexPath.row == 4 || indexPath.row == 5 {
            return UITableView.automaticDimension
        }
        return 200.0
    }

如果有 tableView.estimatedRowHeight = 0 这句代码可不要

    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return 44
    }

相关文章

网友评论

    本文标题:Swift TableView Cell动态高度

    本文链接:https://www.haomeiwen.com/subject/mjzmnqtx.html