私はそこにカスタムセルを持つテーブルを持っています。これらのセルには、画像ビューと2つのラベルが含まれています。私は、典型的なセルのすべてを配置するための制約があります。カスタムセル内のラベル制約を調整するにはどうすればよいですか?
各セルには、ファイルまたはフォルダのいずれかを表します。私が設定したレイアウトはファイルビュー用です(2つのラベルは名前と詳細です)。カスタムセルを作成すると、アイコンがフォルダに変更され、詳細ラベルが非表示になります。私はそれをよりきれいにするために名前ラベルを中心にします。
私の問題は、細胞の再利用から発生します。私は名前ラベルの中心から戻っているようには見えません。私はこの制約を加える方法をいくつか試してみましたが、いつも制約が最初に働くように思えますが、セルが再利用されると問題にぶつかります。セルの
ことの一つは、細胞が新しい中心の制約を削除しようとしたとき、私は唯一の問題を持っている再利用されます(セルがフォルダーセルからファイルセルに移動)
ディレクトリセルクラス
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
は、remove中心に制約を思え、カスタムセルは、この問題に実行して、将来の人々のために
EDIT/SOLUTION
を再利用したとき、トップ制約を適用することはできません、以下の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
}
}
もちろん、ああ。私はそれを刺すだろう。私がやったときに私は戻って報告します – sargturner
ありがとう@ダン私はあなたが示唆したことをした、あなたはまさに正しいです。特に迅速な返事を送ってくれてありがとう! – sargturner