我有一个带有自定义 View 单元格的 UITableViewCell
。
在这个 View 单元格中,我有一个名为 imgWrapper 的简单 UIView
,我在其中添加了如下约束:
- 宽度 = 50
- 高度 = 50
- 导致 super View = 20
- top to superview = 20
- 底部到 super View = 20
这些是那里唯一的限制。我将拥抱和压缩保留为默认值。
在我的代码中我设置了这个:
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 90
}
然后在我的 rowAtIndex 中...我有这个:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: LogCustomCell = tableView.dequeueReusableCellWithIdentifier("logCustomCell", forIndexPath: indexPath) as! LogCustomCell
var imgWrapperHeight: CGFloat = log.big ? 100 : 50
cell.imgWrapperHeight.frame.size.height = imgWrapperHeight
return cell
}
一旦我编译并运行它。所有单元格的大小都相同。
注意事项:
- 我检查了 log.big 是否为真/假,它确实发生了变化。
- 我也尝试过
CGRect(x,y,width,height)
但还是没用。 - 我知道我可以做
heightForRowAtIndexPath
但我想做动画而且我知道我们可以为标签做这样的事情(见打印屏幕)这使得 tableView 知道高度而无需定义它:<
知道我做错了什么吗?
最佳答案
您需要将高度逻辑放入名为 tableView:heightForRowAtIndexPath
的 UITableViewDelegate
方法中,如下所示:
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return (true ? 100 : 50) + 2 * 20
}
附言。我是用 Swift 2 编写的,因此使用了 override
关键字。
关于ios - 动态表格 View 单元格,以编程方式更改 subview 高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32622704/