4

我对单元格使用自动高度。每次我想用新数据(这里的模型变量)更新我的单元格时,我都会更新我的自动布局约束并且我得到一个错误。这里只是为了说明问题,我什至不改变约束。我只是要求重新计算布局。

在单元的第一次初始化时:没有警告,没问题。

错误:

<NSLayoutConstraint:0x174285d70 'UIView-Encapsulated-Layout-Height'
 UITableViewCellContentView:0x1030f8a50.height == 500   (active)>

编码:

tableView.rowHeight = UITableViewAutomaticDimension

override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
      return 500
  }

class TestTableViewCell: UITableViewCell {

  var model: X? {
    didSet {
      setNeedsLayout()
      layoutIfNeeded()
    }
  }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

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

    let viewtop = UIView()
    let viewbottom = UIView()

    viewtop.backgroundColor = UIColor.yellow
    viewbottom.backgroundColor = UIColor.red
    contentView.backgroundColor = UIColor.blue

    contentView.addSubview(viewtop)
    contentView.addSubview(viewbottom)

    viewtop.snp.makeConstraints { (make) in
      make.top.equalTo(contentView)
      make.left.right.equalTo(contentView)
      make.height.equalTo(50)
    }

    viewbottom.snp.makeConstraints { (make) in
      make.top.equalTo(viewtop.snp.bottom)
      make.left.right.equalTo(contentView)
      make.bottom.equalTo(contentView)
      make.height.equalTo(120)
    }
  }

  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }

}

问题:为什么在询问相同约束的重新布局后,我会得到一个错误?

编辑:另一个更好理解的例子。

var botViewHeightConstraint:Constraint!
class TestTableViewCell: UITableViewCell {

  var model: Int? {
    didSet {
      if model == 1 {
        botViewHeightConstraint.update(offset:200)
      }else{
        botViewHeightConstraint.update(offset:120)
      }
    }
  }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

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

    let viewtop = UIView()
    let viewbottom = UIView()

    viewtop.backgroundColor = UIColor.yellow
    viewbottom.backgroundColor = UIColor.red
    contentView.backgroundColor = UIColor.blue

    contentView.addSubview(viewtop)
    contentView.addSubview(viewbottom)

    viewtop.snp.makeConstraints { (make) in
      make.top.equalTo(contentView)
      make.left.right.equalTo(contentView)
      make.height.equalTo(50)
    }

    viewbottom.snp.makeConstraints { (make) in
      make.top.equalTo(viewtop.snp.bottom)
      make.left.right.equalTo(contentView)
      make.bottom.equalTo(contentView)
      botViewHeightConstraint = make.height.equalTo(120).constraint
    }
  }

  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }

}

CellForRow 代码:

 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if let post = fetchedResultsController?.fetchedObjects?[(indexPath as NSIndexPath).section]  {
       let cell = tableView.dequeueReusableCell(withIdentifier: imagePostCellId) as! TestTableViewCell!
       cell.model = 1
       return cell
    }
}
4

2 回答 2

5

首先,您一直忽略将子视图设置translatesAutoresizingMaskIntoConstraintsfalse. 这很重要。(不过,也许 snapkit 会为您做到这一点。我不知道。)

第二——这是重点——你所做的不是你如何从内向外调整高度可变的单元格。您不会像您的代码那样更改绝对高度约束。大小最终基于子视图的内在内容大小。当然,某些子视图可能具有绝对高度,但最终必须至少有一个具有固有内容大小。是您能够动态更改以确定单元格高度的大小。

这就是为什么,例如,包含 UILabel 的单元格非常容易与动态行高一起使用。它具有内在的内容大小。

内在内容大小与单元格的内置高度(在您的控制台转储中)不冲突;UIView-Encapsulated-Layout-Height补充了它(当运行时systemLayoutSizeFitting(UILayoutFittingCompressedSize)在幕后调用时,自动可变行高的工作原理)。

如果您使用具有 实现的自定义子视图intrinsicContentSize,并且如果在单元格中设置模型值会触发对 的调用invalidateIntrinsicContentSize,那么您的示例将完美运行而不会在控制台中出现任何投诉。

以下是此类自定义视图的示例:

class MyView : UIView {
    var h : CGFloat = 200 {
        didSet {
            self.invalidateIntrinsicContentSize()
        }
    }
    override var intrinsicContentSize: CGSize {
        return CGSize(width:300, height:self.h)
    }
}

当这是您的单元格内容视图的子视图时,请正确设置此视图的h大小cellForRow以调整单元格的高度。

例如,假设我们单元格的内容视图只有一个子视图,v即 MyView。然后:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! MyCell
    let even = indexPath.row % 2 == 0
    cell.v.backgroundColor = even ? .red : .green
    cell.v.h = even ? 40 : 80 // triggers layout!
    return cell
}
于 2016-09-26T14:24:29.500 回答
2

当您已经设置了相对于内容和顶视图的上下左右约束时,为什么还要添加“make.height.equalTo(120)”

根据您的代码,单元格高度似乎始终为 50+120。

另请查看是否覆盖 heightForRow 并返回 UITableViewAutomaticDimension 有效。

于 2016-09-26T05:12:23.580 回答