我有一个自定义的UITableViewCell
布局,看起来像这样。它有三个标签。
Label 1 标签1
标签2
标签 3
注意,我添加了一个额外的约束条件,将Y轴与标签1对齐,值为0,并将其优先级设置为750。我认为如果我删除标签2,那个优先级较低的约束条件就会取代它并向下移动。class TableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
// MARK: - Table view data source
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CustomCell
if indexPath.row == 1 {
cell.label2.removeFromSuperview()
cell.updateConstraints()
}
return cell
}
}
但似乎没有起作用。 标签2 被移除了,但是 标签1 的位置仍然不变。
我如何实现我想要的?
尝试 #1
根据 T 先生在下方的 answer,我给 标签 1 添加了一个顶部约束。然后在 cellForRowAtIndexPath
方法中,我改变了它的值。
override func tableView(tableView: UITableView, cellForRowAtIndexPath
indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CustomCell
if indexPath.row == 1 {
cell.label2.removeFromSuperview()
cell.topConstraint.constant = cell.bounds.height / 2
cell.layoutIfNeeded()
}
return cell
}
但这也不起作用。
layoutIfNeeded
吗? - zc246CentreY
约束都针对于superview
,第二个约束应该与标签2有关吗? - zc246