Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift tableViewCell的ContentView未正确自动调整大小?_Swift_Uitableview_Autolayout_Snapkit - Fatal编程技术网

Swift tableViewCell的ContentView未正确自动调整大小?

Swift tableViewCell的ContentView未正确自动调整大小?,swift,uitableview,autolayout,snapkit,Swift,Uitableview,Autolayout,Snapkit,我在单元格中添加了2个标签,并使用snapkit设置了这些约束,问题是我无法使单元格正确展开,它将保持在默认高度: titleLabel.snp.makeConstraints { (make) -> Void in make.top.equalTo(contentView.snp.top) make.bottom.equalTo(descriptionLabel.snp.top) make.left.equalTo(contentView.

我在单元格中添加了2个标签,并使用snapkit设置了这些约束,问题是我无法使单元格正确展开,它将保持在默认高度:

titleLabel.snp.makeConstraints { (make) -> Void in
        make.top.equalTo(contentView.snp.top)
        make.bottom.equalTo(descriptionLabel.snp.top)
        make.left.equalTo(contentView.snp.left)
        make.right.equalTo(contentView.snp.right)
    }
    descriptionLabel.snp.makeConstraints { (make) -> Void in
        make.top.equalTo(titleLabel.snp.bottom)
        make.bottom.equalTo(contentView.snp.bottom)
        make.left.equalTo(contentView.snp.left)
        make.right.equalTo(contentView.snp.right)
    }
正如你所看到的,我映射了四条边,但是我知道这些并不意味着高度,当内容本质上是动态的,并且可能是各种高度时,我如何应用高度

标签的设置如下所示:

 lazy var titleLabel: UILabel = {
    let titleLabel = UILabel()
    titleLabel.textColor = .green
    titleLabel.textAlignment = .center
    contentView.addSubview(titleLabel)
    return titleLabel
}()

lazy var descriptionLabel: UILabel = {
    let descriptionLabel = UILabel()
    descriptionLabel.textColor = .dark
    descriptionLabel.textAlignment = .center
    descriptionLabel.numberOfLines = 0
    contentView.addSubview(descriptionLabel)
    return descriptionLabel
}()

为表视图提供一个
estimatedRowHeight
,并将其
rowHeight
设置为UITableViewAutomaticDimension。现在,单元格将自动调整大小。如果标签的四面都固定在内容视图上,并且单元格是自调整大小的,那么您只需这样做:标签将自动更改其高度以适应其文本,并且单元格将自动更改大小以适应标签。

首先,我认为应该在子类UITableViewCell类初始值设定项方法中将子视图添加到contentView

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
           super.init(style: style, reuseIdentifier: reuseIdentifier)
           self.contentView.addSubview(titleLabel)
           self.contentView.addSubview(descriptionLabel)
}
其次,确保在viewDidLoad方法(可能在ViewController中)中添加以下两行:

tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableView.automaticDimension
当然,您应该更改estimatedRowHeight以适应您的需要

还有一件事值得一提——您可以更轻松地创建这些约束(使用SnapKit的强大功能):


这里的可下载示例项目:是的,那么我所做的是正确的吗?但是它不起作用!细胞保留了它的大小嘿,我给你看了我的代码,它确实工作了。下载项目并查看!您还没有显示代码(我不知道如何配置表视图),所以谁知道您做错了什么。
titleLabel.snp.makeConstraints { (make) -> Void in
    make.top.left.right.equalTo(contentView)
}
descriptionLabel.snp.makeConstraints { (make) -> Void in
    make.top.equalTo(titleLabel.snp.bottom)
    make.bottom.left.right.equalTo(contentView)
}