如何在Swift中扩展Table View Cell的高度?

时间:2015-10-04 08:47:06

标签: ios swift tableview swift2 tableviewcell

我有一个TableViewController,想要在按下按钮时更改其中一个静态单元格的height。我创建了一个Outlet of my Cell:

@IBOutlet var myCell: UITableViewCell!

问题是,我没有属性高度,所以我可以这样做。像这样:

@IBAction func btnClick(sender: UIButton) {
    myCell.rowHeight = 200
}

我知道有一种方法叫做:

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    ...
}

但是当我按下UIButton时如何触发此方法?

1 个答案:

答案 0 :(得分:3)

tableView.beginUpdates()后跟tableView.endUpdates()会触发委托方法调用,例如tableView:heightForRowAtIndexPath:

所以你可以这样做:

var height = 100

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return height
}

@IBAction func btnClick(sender: UIButton) {
    tableView.beginUpdates()
    height = 200
    tableView.endUpdates()
}
相关问题