如何为具有动态高度的表格单元格正确设置自动布局? [英] How to set auto layout correctly for a table cell with dynamic height?

查看:23
本文介绍了如何为具有动态高度的表格单元格正确设置自动布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试创建一个 UITableViewCell 并设置子视图如下:

I try to create a UITableViewCell and setup subviews as below:

class MyCellView: UITableViewCell {

    private let ImageView = UIImageView()
    private let titleView = UILabel()

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {

        super.init(style: style, reuseIdentifier: reuseIdentifier)

        contentView.addSubview(ImageView)
        contentView.addSubview(titleView)

        ImageView.translatesAutoresizingMaskIntoConstraints = false
        ImageView.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
        ImageView.leftAnchor.constraint(equalTo: contentView.leftAnchor).isActive = true
        ImageView.rightAnchor.constraint(equalTo: contentView.rightAnchor).isActive = true
        ImageView.heightAnchor.constraint(equalTo: contentView.widthAnchor, multiplier: 0.6).isActive = true

        titleView.translatesAutoresizingMaskIntoConstraints = false
        titleView.topAnchor.constraint(equalTo: ImageView.bottomAnchor).isActive = true
        titleView.leftAnchor.constraint(equalTo: contentView.leftAnchor).isActive = true
        titleView.rightAnchor.constraint(equalTo: contentView.rightAnchor).isActive = true
        titleView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true

        titleView.numberOfLines = 0

        titleView.text = " "   // <-- *** please note this line

    }
}

在表格视图中,我已将其设置为使用动态高度:

And in the table view, I have set it to use dynamic height:

tableView.estimatedRowHeight = 80
tableView.rowHeight = UITableViewAutomaticDimension

我的问题是如果我没有在单元格中为 titleView 设置文本,例如如果我在单元格视图中注释最后一行:

My question is if I don't set the text for titleView in the cell, such as if I remark the last line in the cell view:

// titleView.text = " "

我将从 XCode 收到约束错误.但是如果我设置文本,一切正常.

I will get constraint error from XCode. But if I set the text, everything works fine.

文本是从网上动态下载的,所以文本可能是空的.另外我可以将文本设置为"",一个空格,不知道我在这里误解了什么?如果我更正某些内容,文本可以为空("" 或 nil)吗?

The text is dynamically downloaded from the Internet, so the text might be empty. Besides I can just set the text to " ", an empty space, I wonder if I misunderstand anything here? Can the text be empty ("" or nil) if I correct something?

同样,如果我完全删除关于 titleView 的代码,我只想在单元格中有一个 UIImageView,我发现单元格的高度没有扩展.如何设置它以便我可以在单元格中获得动态高度的图像?

Similarly, if I remove the code about titleView entirely, I just want to have an UIImageView in the cell, I found the cell's height is not expanded. How to set it up so that I can have an image of dynamic height in the cell?

约束错误:

2017-12-22 15:35:34.715865+0800 MyProject[15774:733515] [LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(
"<NSLayoutConstraint:0x60c000095450 V:|-(0)-[UIImageView:0x7ff40df0de90]   (active, names: '|':UITableViewCellContentView:0x7ff40df0edc0 )>",
"<NSLayoutConstraint:0x60c0000955e0 UIImageView:0x7ff40df0de90.height == 0.6*UITableViewCellContentView:0x7ff40df0edc0.width   (active)>",
"<NSLayoutConstraint:0x60c000095630 V:[UIImageView:0x7ff40df0de90]-(0)-[UILabel:0x7ff40df0e0c0]   (active)>",
"<NSLayoutConstraint:0x60c0000957c0 UILabel:0x7ff40df0e0c0.bottom == UITableViewCellContentView:0x7ff40df0edc0.bottom   (active)>",
"<NSLayoutConstraint:0x60c000096210 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7ff40df0edc0.height == 248.333   (active)>",
"<NSLayoutConstraint:0x60c000095ea0 'UIView-Encapsulated-Layout-Width' UITableViewCellContentView:0x7ff40df0edc0.width == 414   (active)>"
)

Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x60c000095630 V:[UIImageView:0x7ff40df0de90]-(0)-[UILabel:0x7ff40df0e0c0]   (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

推荐答案

  • 如果你想避免警告,试试下面的代码

    • If you want to avoid warning, try the code below

      class MyCellView: UITableViewCell {
      
        private let ImageView = UIImageView()
        private let titleView = UILabel()
      
        override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
      
          super.init(style: style, reuseIdentifier: reuseIdentifier)
      
          contentView.addSubview(ImageView)
          contentView.addSubview(titleView)
      
          ImageView.translatesAutoresizingMaskIntoConstraints = false
          ImageView.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
          ImageView.leftAnchor.constraint(equalTo: contentView.leftAnchor).isActive = true
          ImageView.rightAnchor.constraint(equalTo: contentView.rightAnchor).isActive = true
          let heightConstraint = ImageView.heightAnchor.constraint(equalTo: contentView.widthAnchor, multiplier: 0.6)
          heightConstraint.priority = UILayoutPriority.defaultLow
          heightConstraint.isActive = true
      
          titleView.translatesAutoresizingMaskIntoConstraints = false
          titleView.topAnchor.constraint(equalTo: ImageView.bottomAnchor).isActive = true
          titleView.leftAnchor.constraint(equalTo: contentView.leftAnchor).isActive = true
          titleView.rightAnchor.constraint(equalTo: contentView.rightAnchor).isActive = true
          titleView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
      
          titleView.numberOfLines = 0
      
      //    titleView.text = " "
        }
      }
      

    • 如果您完全删除了关于 titleView 的代码,并希望在单元格中有一个具有动态高度的 UIImageView,请添加 bottomAnchorimageView

    • If you remove the code about titleView entirely and want to have an UIImageView with dynamic height in the cell, adding bottomAnchor constraint for imageView

      class MyCellView: UITableViewCell {
      
        private let ImageView = UIImageView()
        private let titleView = UILabel()
      
        override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
      
          super.init(style: style, reuseIdentifier: reuseIdentifier)
      
          contentView.addSubview(ImageView)
          contentView.addSubview(titleView)
      
          ImageView.translatesAutoresizingMaskIntoConstraints = false
          ImageView.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
          ImageView.leftAnchor.constraint(equalTo: contentView.leftAnchor).isActive = true
          ImageView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
          ImageView.rightAnchor.constraint(equalTo: contentView.rightAnchor).isActive = true
          let heightConstraint = ImageView.heightAnchor.constraint(equalTo: contentView.widthAnchor, multiplier: 0.6)
          heightConstraint.priority = UILayoutPriority.defaultLow
          heightConstraint.isActive = true
        }
      }
      

    • 这篇关于如何为具有动态高度的表格单元格正确设置自动布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆