【发布时间】:2017-02-02 15:53:18
【问题描述】:
在浏览完所有其他 Stack Overflow 表单后,我为我的一个单元格实现了一个动态高度,如下所示:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as UITableViewCell
if(indexPath.row == 1){
var image : UIImage = maskRoundedImage(image: UIImage(named: "Temp_Profile.png")!, radius: 150)
cell.imageView?.image = image
return cell
}
cell.textLabel?.text = TableArray[indexPath.row]
cell.backgroundColor = UIColor.init(colorLiteralRed: 0.56, green: 0, blue: 0.035, alpha: 1)
cell.textLabel?.textColor = UIColor.white
cell.textLabel?.font = UIFont(name: "Helvetica", size: 28)
return cell
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath.row == 1 {
return UITableViewAutomaticDimension * 3
}
return UITableViewAutomaticDimension
}
看起来很简单,但调试会话显示 heightForRowAtIndexPath 从未被调用。 View Controller 是 UITableViewController ,其他一切正常。你们有没有看到这个函数没有被调用的任何原因?
感谢您的帮助!
【问题讨论】:
-
如下所述,UITableViewAutomaticDimension 不是应该用于算术的值。返回 UITableViewAutomaticDimension * 3 会产生不可预知的效果。
-
请注意,调用
heightForRowAt可能很昂贵;您也可以在viewDidLoad中致电self.tableView.rowHeight = UITableViewAutomaticDimension和self.tableView.estimatedRowHeight = 72.0 //estimated, average value。也可以在这里查看:appcoda.com/self-sizing-cells
标签: ios swift uitableview heightforrowatindexpath