ios - uitableviewcell 内的 imageview 折叠单元格

标签 ios swift uitableview ios-autolayout

这是带有 ImageView 的表格 View 单元格

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

}

这是表格 View 的代码
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
    }

这是控制台中显示的错误

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

最佳答案

您可以使用 contentView ,这不是绝对必要的,我直接使用self.addSubview()
由于图像的高度大于标准行的高度。您要么必须使用 UITableViewDelegate 来实现高度方法heightForRow高度大于您的55或者你添加一个底部 anchor :userImageView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true

关于ios - uitableviewcell 内的 imageview 折叠单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58441964/

相关文章:

ios - 谓词 "IN"在设备上非常慢(相差 83 倍)

ios - Siren库如何获取当前的应用商店版本?

objective-c - PushViewController 失败

Swift - 记录 mac 应用程序中的所有请求

swift - 将 SwiftUI View 添加到 UITableViewCell contentView

ios - 图像作为每个 View 的背景

ios - 如何从谷歌地图获取省份和城市 - ios

ios - 如何使用 swift 将批量数据插入 iOS 中的 SQLite 数据库

json - 从 TableView 中的 JSON 数据下载可重复使用的单元格或错误的异步图像

ios - UITableViewCell UILabel 多行对齐