【发布时间】:2016-06-28 23:51:45
【问题描述】:
这是我的问题。我在 UITableView 中使用自定义 UITableViewCell 作为标识符为“cellidentifier”的 Cell 原型。在这个 UITableViewCell 中,我添加了(在 Interface Builder 中)一个空的 UIView 到 contentView 的单元格中,我称之为“bubbleView”,它有一个连接 @IBOutlet,我添加了约束 top = 0、bottom = 0、leading = 0 和 trailing = 0。
在控制器(继承自 UITableViewDataSource 和 UITableViewDelegate)中,在 viewDidLoad 函数中我添加了以下几行:
override func viewDidLoad() {
...
self.mytable.delegate = self
self.mytable.dataSource = self
self.mytable.estimatedRowHeight = 100.0
self.mytable.rowHeight = UITableViewAutomaticDimension;
...
}
然后,我以这种方式实现了 UITableViewDelegate 的方法:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("cellidentifier") as! myCustomCell!
if (cell == nil) {
cell = myCustomCell(style:UITableViewCellStyle.Subtitle, reuseIdentifier:"cellidentifier")
}
let myNewView = UIView(frame: CGRectMake(0, 10, randomWidth, random Height))
cell.bubbleView.addSubview(myNewView)
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
使用该代码,我可以将 myNewView 添加到单元格中,但单元格不会根据 myNewView 高度调整到实际高度,例如,如果 myNewView 高度为 134,我的自定义视图单元格总是返回 44 高度。正如一些教程所说,我添加了 layoutIfNeeded 属性,但它不起作用。有人有这个解决方案吗?
【问题讨论】:
标签: ios swift uitableview swift2