我在故事板上更改约束的关系时遇到了麻烦 . 这就是表视图目前的样子 .
单击更多按钮时,我希望表视图看起来像这样 .
好的,我将底部约束从imageView拖到我的tableViewCell.swift文件中,并将其命名为 imageBottomConstraint . 我试过了 imageBottomConstraint.relation = .greaterThan ,但是给了我一个错误,说它试图将它改为更大的东西's only a get, so the relation is equalTo and I' .
这是我试图创建一个新的约束,但不是真的理解 NSLayoutConstraint(item:, attribute:, relatedBy:, toItem:, attribute:, multiplier:, constant:) . 不懂项目,属性,relatedBy,toItem&attribute,有人可以帮忙澄清一下 .
我认为我的灾难代码会起作用,但不会崩溃 .
@IBAction func showMoreBtn() {
let item = imageBottomConstraint.firstItem
let attribute1 = imageBottomConstraint.firstAttribute
let relatedBy = NSLayoutRelation.greaterThanOrEqual
let item2 = imageBottomConstraint.secondItem
let attribute2 = imageBottomConstraint.secondAttribute
let multiplier = imageBottomConstraint.multiplier
let constant = 12
let newConstraint = NSLayoutConstraint(item: item, attribute: attribute1, relatedBy: relatedBy, toItem: toItem, attribute: attribute2, multiplier: multiplier, constant: 12)
}
因此,当您创建约束程序时,您可以看到我不知道我在做什么 . 很感激帮助 .
2 回答
您可以在TableView的heightForRowAt Cell Delegate中管理预期行为
selectedIndex 其中更多Button被点击的索引,我的要求是扩展一个单元格一次,你可以使用一个数组来管理单元格需要扩展
您可能无法使用正确的解决方案 . 我会通过使用
UITableViewAutomaticDimension
来使用单元格可以扩展并适应内容的事实 . 这是一个可以帮助您解决特定情况的示例:https://www.appcoda.com/self-sizing-cells/
这个想法是:
使用约束来修复标签的高度
单击更多按钮时,使用
yourConstraint.active = false
将该约束设置为不活动,并通过执行类似yourTableView.reloadRows
的操作重新加载单元格