【问题标题】:imageview inside uitableviewcell collapses the celluitableviewcell 内的 imageview 折叠单元格
【发布时间】:2020-02-14 22:13:44
【问题描述】:

这是带有图像视图的表格视图单元格

class ChatTableViewCell: UITableViewCell {

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


        self.contentView.addSubview(userImageView)

        userImageView_constraints()

        }

        var userImageView: UIImageView = {
        let imageView = UIImageView()
        let imageViewHeightAndWidth: CGFloat = 55
        let image = UIImage(named: "steve")
        imageView.image = image
        imageView.clipsToBounds = true
        imageView.layer.cornerRadius = imageViewHeightAndWidth / 2
        imageView.translatesAutoresizingMaskIntoConstraints = false
        return imageView
    }()


func userImageView_constraints(){
    userImageView.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant: 20).isActive = true
    userImageView.topAnchor.constraint(equalTo: self.contentView.topAnchor, constant: 0).isActive = true
    userImageView.widthAnchor.constraint(equalToConstant: 55).isActive=true
    userImageView.heightAnchor.constraint(equalToConstant: 55).isActive=true

}

这是表格视图的代码

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "chatCell", for: indexPath) as! ChatTableViewCell
        return cell
    }

这是控制台中显示的错误

[警告] 仅警告一次:检测到约束模糊地建议表格视图单元格的内容视图高度为零的情况。我们正在考虑意外折叠并使用标准高度。单元格:>

【问题讨论】:

  • 添加一个从图像视图底部到内容视图底部锚点的底部约束。如果需要,您可以将其设置为低优先级以允许其他内容影响高度
  • 这只是打破了我的图像视图约束将尝试通过打破约束来恢复 <0x281e1a530 uiimageview:0x10132ca80.height="=">

标签: ios swift uitableview ios-autolayout


【解决方案1】:

你可以用contentView,不是绝对必要,我直接用self.addSubview()

由于图像的高度大于标准行的高度。您要么必须使用UITableViewDelegate 方法heightForRow 实现高度大于55 的高度,要么添加底部锚点:userImageView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true

【讨论】:

    猜你喜欢
    • 2023-03-05
    • 2017-12-23
    • 2011-06-09
    • 2013-06-07
    • 1970-01-01
    • 1970-01-01
    • 2022-10-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多