可扩展的细胞,自我上浆的高度

时间:2016-12-19 15:46:17

标签: ios swift uitableview uiview uilabel

我使用动态金额UILabels创建了可扩展视图(根据查询生成的结果计数以编程方式创建)。但是,我无法根据展开视图中的UILabels数量来调整展开的视图高度。

使用for loop我制作的标签与查询后的结果一样多。

现在我把它们放到这样看来:

for (index, _) in timesBetween2Stations.enumerated() {
            let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 20))
            cell.ExpandableView.addSubview(label)
            label.translatesAutoresizingMaskIntoConstraints = false
            label.center = CGPoint(x: cell.ExpandableView.frame.width / 2, y: 0)
            label.textAlignment = NSTextAlignment.center
            var substationTime = timesBetween2Stations[index]?.time
            let substation = timesBetween2Stations[index]?.station
            substationTime = substationTime!.removeTrainTimeZeros()
            label.text = substation! + " " + substationTime!
            cell.ExpandableView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-(10)-[label]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["label": label, "expandable": cell.ExpandableView]))
            if (previousLabel == nil){
                cell.ExpandableView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-(10)-[label]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["label": label, "expandable": cell.ExpandableView]))
            }
            else {
                cell.ExpandableView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[previousLabel]-(10)-[label]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["label": label, "previousLabel": previousLabel as Any]))
            }
            previousLabel = label

            }

问题是 - 为什么它没有根据约束调整视图高度? :)

1 个答案:

答案 0 :(得分:0)

试试这个:

cell.setNeedsLayout()
cell.layoutIfNeeded()

cell.setNeedsUpdateConstraints()
cell.updateConstraintsIfNeeded()

lor之后。