简体   繁体   English

动态更改uitableview页脚

[英]change uitableview footer dynamically

I have a custom UITableViewCell that I am using as footer for UITableView. 我有一个自定义UITableViewCell,正在用作UITableView的页脚。

func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    let cell = tableView.dequeueReusableCellWithIdentifier("footerCommentCell") as! FooterCommentTableViewCell
    cell.footerIndex = section
    cell.delegate = self
    return cell
}

This footer has autolayout like http://joxi.ru/DrloGbpC4Eb3vA 1 - view 1 2 - view 2 3 - view 2 height constraint ( view2Height ) 此页脚具有自动布局,例如http://joxi.ru/DrloGbpC4Eb3vA 1-视图1 2-视图2 3-视图2高度限制( view2Height

At init phase view2Height is 0, and for some actions I want to change this constraint constant to another value, for example - 50, and make my header changed to. 在初始化阶段, view2Height为0,对于某些操作,我想将此约束常量更改为另一个值,例如-50,并将标头更改为。

Is there any way to implement this? 有什么办法可以实现呢?

You can override heightForFooterInSection and return 0 or 50 based on the requirement and reload table section. 您可以覆盖heightForFooterInSection并根据需求和重载表部分返回0或50。

override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {

        //return value based on condition 
    }

This is very easy. 这很容易。 When you say dynamic it means you need some condition to change it. 当您说动态时,这意味着您需要一些条件来对其进行更改。 For ex : Subclass two UITableviewCells and then just reload the table view. 例如:子类化两个UITableviewCells,然后重新加载表视图。

Adding to @2ank3th answer, you can get the cell/view of footer. 添加到@ 2ank3th答案,您可以获得页脚的单元格/视图。

func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    let footerView = tableView.footerViewForSection(0) as! FooterCommentTableViewCell 
// Your section number. (0 for understanding purpose)

// Then you can modify the height constraint and return the height of footer here.

}
func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat 
{
    return UITableViewAutomaticDimension
}

func tableView(tableView: UITableView, estimatedHeightForFooterInSection section: Int) -> CGFloat
{
    return 44.0
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM