【问题标题】:How to expand the height of a Table View Cell in Swift?如何在 Swift 中扩展表格视图单元格的高度?
【发布时间】:2015-10-04 08:47:05
【问题描述】:

我有一个TableViewController,并且想在按下按钮时更改我的一个静态单元格的height。我创建了我的单元格的 Outlet:

@IBOutlet var myCell: UITableViewCell!

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

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

我知道有一个方法叫:

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

但是当我按下 UIButton 时如何触发这个方法呢?

【问题讨论】:

    标签: ios swift tableview swift2 tableviewcell


    【解决方案1】:

    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()
    }
    

    【讨论】:

    • 如果我没有错,当重新加载 rhings 应该发生在静态 tableView 上时,您需要覆盖每个委托和数据源函数。在其他委托和数据源函数中只需调用 super.tableView...
    • 谢谢,这正是我所需要的。
    猜你喜欢
    • 2021-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-28
    • 1970-01-01
    • 2021-01-21
    • 1970-01-01
    相关资源
    最近更新 更多