如何在自定义单元格中调整标签约束?

时间:2016-12-07 19:25:55

标签: ios swift uitableview constraints

我有一张桌子里面有自定义单元格。这些单元格包含图像视图和两个标签。我有限制来为一个典型的细胞定位一切。

Dynamic Cell Layout

每个单元格代表一个文件或一个文件夹。我设置的布局是用于文件视图(两个标签是名称和详细信息)。当我创建自定义单元格时,我将图标更改为文件夹,并且细节标签将隐藏。然后,我将名称标签居中,使其更漂亮。

我的问题来自重复使用细胞。我似乎无法从名称标签的中心恢复。我尝试了几种不同的添加此约束的方法,并且似乎总是能够让约束第一次工作,但是一旦重用了一个单元格,我就会遇到问题。

首次创建单元格 enter image description here

重复使用单元格后的问题 enter image description here

我注意到的一件事是,当一个单元格试图删除新的中心约束(单元格从文件夹单元格转到文件单元格)时,我只有问题

目录单元类

class DirectoryCell: UITableViewCell {

    @IBOutlet weak var directoryTypeImage: UIImageView!
    @IBOutlet weak var directoryNameLabel: UILabel!
    @IBOutlet weak var directoryDetailsLabel: UILabel!

    var directoryItem: DirectoryItem! {
        didSet {
            self.updateUI()
        }
    }

    func updateUI() {
        let centerConstraint = NSLayoutConstraint(item: directoryNameLabel, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: self.contentView, attribute: NSLayoutAttribute.centerY, multiplier: 1.0, constant: 0.0)
        let topConstraint = NSLayoutConstraint(item: directoryNameLabel, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: self.contentView, attribute: NSLayoutAttribute.top, multiplier: 1.0, constant: 7.0)

        directoryNameLabel.text = directoryItem.name
        directoryTypeImage.image = directoryItem.typeIcon

        if (directoryItem.type == DirectoryItem.types.FOLDER) {
            self.removeConstraint(topConstraint)
            self.addConstraint(centerConstraint)

            directoryDetailsLabel.isHidden = true
        } else {
            self.removeConstraint(centerConstraint)
            self.addConstraint(topConstraint)

            directoryDetailsLabel.text = directoryItem.details
            directoryDetailsLabel.isHidden = false
        }
    }
}

我只是简单地应用/删除约束错误,或者在错误的位置应用/删除它们?

当我浏览调试器并查看self.constraints表达式时,我没有受到约束。我在哪里误解了我的自定义单元格的限制?

TL; DR

似乎无法删除居中约束并在重复使用自定义单元格时应用顶部约束

修改/溶液

对于遇到此问题的任何未来人员,dan的答案完全正确。我需要为我想要应用的每个约束创建一个属性。然后我删除所有约束并仅应用我想要的约束。

已添加到DirectoryCell类

var topConstraint: NSLayoutConstraint {
    get {
        return NSLayoutConstraint(item: self.directoryNameLabel, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: self.contentView, attribute: NSLayoutAttribute.top, multiplier: 1.0, constant: 7.0)
    }
}
var centerConstraint: NSLayoutConstraint {
    get {
        return NSLayoutConstraint(item: self.directoryNameLabel, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: self.contentView, attribute: NSLayoutAttribute.centerY, multiplier: 1.0, constant: 0.0)
    }
}

新的updateUI()

func updateUI() {
    directoryNameLabel.text = directoryItem.name
    directoryTypeImage.image = directoryItem.typeIcon

    if (directoryItem.type == DirectoryItem.types.FOLDER) {
        self.removeConstraints(self.constraints) // Remove all constraints
        self.addConstraint(centerConstraint) // Add constraint I want for this "cell type"

        directoryDetailsLabel.isHidden = true
    } else {
        self.removeConstraints(self.constraints)
        self.addConstraint(topConstraint)

        directoryDetailsLabel.text = directoryItem.details
        directoryDetailsLabel.isHidden = false
    }
}

1 个答案:

答案 0 :(得分:1)

您实际上并未删除第一次updateUI运行时添加的约束,而是创建一个永不添加的新居中约束并删除该约束。因此,当您重复使用时,您的单元格中心和顶部都有约束,并且居中约束显然会赢得冲突。

您需要创建一次centerConstrainttopConstraint并将其存储在单元格的属性中,然后在updateUI中添加或删除这些属性。