这是带有 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/