【问题标题】:UITableViewCell with embedded vertical Stack View setup with auto layout and dynamic heightUITableViewCell 具有嵌入式垂直堆栈视图设置,具有自动布局和动态高度
【发布时间】:2018-04-29 10:43:17
【问题描述】:

我正在尝试使用嵌入在单元格中的堆栈视图制作一个动态单元格,因此当将项目添加到单元格时,单元格的高度会适应堆栈视图的高度,但没有得到预期的结果,这意味着我会受到严格的限制。 以下是如何设置设置。我有一个带有嵌入式堆栈视图的单元格:

class MyCell: UITableViewCell {

override var reuseIdentifier: String? {
    return "cell"
}

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
}

    @IBOutlet public weak var stack: UIStackView?

}

这是笔尖的样子:

在表格视图代码中我有以下内容:

 func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

public  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return 1
}

public  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! MyCell

    DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(2)) {

        for _ in 0...10 {
            let view = MyView()
            cell.stack!.addArrangedSubview(view)
        }
    }
    return cell
}

MyView 在哪里:

class MyView: UIView {

override var intrinsicContentSize: CGSize {

    setContentHuggingPriority(.defaultHigh, for: .vertical)


    backgroundColor = .blue
    return CGSize(width: 50, height: 50)
}
}

【问题讨论】:

    标签: uitableview autolayout interface-builder uistackview


    【解决方案1】:

    似乎错误来自将堆栈视图的底部锚定到内容视图的底部,而不是应该将堆栈上最后一个视图的底部锚定到内容视图的底部。

    【讨论】:

      猜你喜欢
      • 2021-10-12
      • 1970-01-01
      • 2017-04-07
      • 2018-03-08
      • 1970-01-01
      • 1970-01-01
      • 2014-10-19
      • 2018-05-23
      • 2013-05-25
      相关资源
      最近更新 更多