Swift - 可扩展 TableViewCell 中的约束问题 [英] Swift - Constraints issue in expandable TableViewCell

查看:97
本文介绍了Swift - 可扩展 TableViewCell 中的约束问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个带有标签和选择器视图的可扩展单元

I'm creating an expandable cell with labels and picker view

xib

单元实现

class PickerViewCell: UITableViewCell {

@IBOutlet weak var detailLabel: UILabel!
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var height: NSLayoutConstraint!
@IBOutlet weak var picker: UIDatePicker!
var isExpanded:Bool = false
    {
        didSet
        {
            if isExpanded == false{
                self.height.constant = 0.0
                self.picker.hidden = true

            }
            else{
                self.height.constant = 200.0
                self.picker.hidden = false
            }
        }
}
override func awakeFromNib() {
    self.height.constant = 0.0
    super.awakeFromNib()
}

委托方法

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    guard let cell = tableView.cellForRowAtIndexPath(indexPath) as? PickerViewCell else { return }

    cell.isExpanded = !cell.isExpanded
    print(cell.isExpanded)
    UIView.animateWithDuration(0.3) {
        cell.contentView.layoutIfNeeded()
    }

    tableView.beginUpdates()
    tableView.endUpdates()

    tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: UITableViewScrollPosition.Top, animated: true)
}

我不断收到关于打破约束的运行时警告.

I keep getting runtime warining about breaking constraints.

我设置了一个符号断点,我发现这一行

I set up a symbolic breakpoint and what I found out is that this line

self.height.constant = 200.0

inside cell implementation 是警告的原因.

inside cell implementation is the reason of the warning.

但是,我发现它只在给定单元格的第一次点击期间发生,在初始点击后我可以点击多次,每次扩展和关闭它,并且不会出现警告.我应该关心这个运行时警告吗?

我忘记显示 datePicker 约束,包括高度和纵横比:

I forgot to show datePicker constraints including those on height and aspect ratio:

另外值得一提的是我使用了

Also it's worth to mention that I utilize

self.tableView.estimatedRowHeight = 100.0
self.tableView.rowHeight = UITableViewAutomaticDimension

在我的 viewController

推荐答案

我之前尝试过将高度约束的优先级降低到 750 及以下,但没有奏效.

I tried before to lower the priority of height constraint to 750 and lower, that did not work.

但是,感谢这个答案https://stackoverflow.com/a/30333810/4286947 我将其设置为 999,结果证明这是这里的解决方案

这篇关于Swift - 可扩展 TableViewCell 中的约束问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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