约束在UITableViewCell上满足所有约束时中断

时间:2015-07-16 17:10:38

标签: ios swift uitableview autolayout

我在界面构建器中有这个单元格约束:

Cell Constrains on IB

这是关于UITableViewCell的代码:

public override func updateConstraints() {
    super.updateConstraints()
    self.contentLabel.preferredMaxLayoutWidth = CGRectGetWidth(UIScreen.mainScreen().bounds) - CGRectGetMinX(self.contentLabel.frame)*2
}

唯一没有高度约束的元素是标签,当TableViewCell增加高度时,应该扩展标签以处理文本扩展。

然而,我收到此错误日志:

(
    "<NSLayoutConstraint:0x1754972f0 UILabel:0x141233ad0.height == 11>",
    "<NSLayoutConstraint:0x175497d40 MKMapView:0x141233e30.height == 80>",
    "<NSLayoutConstraint:0x17149b350 UILabel:0x141233ad0.top == UITableViewCellContentView:0x141218870.top + 16>",
    "<NSLayoutConstraint:0x17149b440 MKMapView:0x141233e30.top == UILabel:0x141233ad0.bottom + 13>",
    "<NSLayoutConstraint:0x171499280 UITableViewCellContentView:0x141218870.bottom == UILabel:0x141233c80.bottom + 8>",
    "<NSLayoutConstraint:0x171499640 UILabel:0x141233c80.top == MKMapView:0x141233e30.bottom + 8>",
    "<NSLayoutConstraint:0x1714997d0 UITableViewCellContentView:0x141218870.height == 44>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x175497d40 MKMapView:0x141233e30.height == 80>

好吧,它根本不会破坏我的屏幕,但它表明事情并没有按照自己的意愿运作。

要提供更多信息,请参阅UITableViewDataSource / Delegate的代码

    public func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    if indexPath.row == 0 {
        let cell = UITableViewCell()
        cell.selectionStyle = UITableViewCellSelectionStyle.None
        cell.backgroundColor = UIColor.whiteColor()
        return cell
    }


    if let unwrapedUnit = self.viewModel.unit {
        let kind = self.viewModel.availableCells[indexPath.row - 1]
        var cell = tableView.dequeueReusableCellWithIdentifier(kind.identifier.rawValue) as! UnitDetailTableViewCell
        cell.selectionStyle = UITableViewCellSelectionStyle.None
        switch kind.cellInfoKind {

        case CellInfoKind.Map:
            let mapCell = cell as! UnitDetailMapTableViewCell

            mapCell.titleLabel.text = "mapa"
            mapCell.contentLabel.text = "addres \n City + State"

            mapCell.coordinate = CLLocationCoordinate2D(latitude: CLLocationDegrees(unwrapedUnit.lat), longitude: CLLocationDegrees(unwrapedUnit.lng))

            cell = mapCell


        /*...*/            
        default:
            return cell

        }
        cell.sizeToFit()
        cell.setNeedsUpdateConstraints()
        cell.updateConstraintsIfNeeded()
        cell.updateConstraints()

        return cell
    }
    return UITableViewCell()

}

// I have the same implementation on tableView:estimatedHeightForRowAtIndexPath:
enter code here
public func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

    if indexPath.row == 0 {
        return CGRectGetHeight(self.titleView.frame)
    }

    let cell = self.tableView.dataSource?.tableView(tableView, cellForRowAtIndexPath: indexPath) as! UnitDetailTableViewCell
    let size = cell.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize)
    return size.height+1
}

那么......知道发生了什么事吗?

1 个答案:

答案 0 :(得分:0)

您需要删除autoresizing代码,因为您使用的是autolayout

也无需致电

    cell.sizeToFit()
    cell.setNeedsUpdateConstraints()
    cell.updateConstraintsIfNeeded()
    cell.updateConstraints()

系统将负责布局视图并设置约束,您不是第一次更新约束

此外,您可能希望将preferredMaxLayoutWidth的代码移至layoutSubviews(),无需覆盖updateConstraints()

override func layoutSubviews() {
    super.layoutSubviews()
    self.contentLabel.preferredMaxLayoutWidth = label.bounds.width
}