【问题标题】:How to set set height of some cells manually when using UITableViewAutomaticDimension?使用 UITableViewAutomaticDimension 时如何手动设置某些单元格的设置高度?
【发布时间】:2016-09-16 13:45:00
【问题描述】:

我正在使用 UITableViewAutomaticDimension 来设置我的 UITableView 单元格的高度。对于过滤功能,我需要隐藏一些单元格,我将通过将它们的高度设置为 0 来隐藏它们。

但是,当我覆盖 heightForRowAtIndexPath 时,我需要为我想要自动调整大小的行返回一些值。如何设置?

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 50

,

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if shouldHideCell == true{
        return 0
    }else{
        return automaticHeight // I need a value here!!!
    }
}

【问题讨论】:

  • 好问题。精彩的答案。太好了,不是唯一的一个!谢谢 :-) :-) 100 万张赞成票,如果可以的话。

标签: ios swift uitableview


【解决方案1】:

在您的情况下,请尝试在方法中设置 UITableViewAutomaticDimension 而不是在您的 viewDidload 中设置:

override func viewDidLoad() {
     super.viewDidLoad()
     tableView.estimatedRowHeight = 50
     ....
}


func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if shouldHideCell == true{
        return 0.0
    }else{
        return UITableViewAutomaticDimension
    }
}

我希望您使用autoLayoutsUITableViewAutomaticDimension 一起工作。 请注意 heightForRowAtIndexcellForRow 方法之前被调用,因此请确保在此之前正确设置您的变量 shouldHideCell

【讨论】:

    【解决方案2】:

    如果您使用UITableViewAutomaticDimension,我想您使用的是自动布局,对吧? 如果您使用自动布局,您可以通过更改具有高度或固有高度的约束的常量来“隐藏”您的单元格。 试试这个。

    【讨论】:

    • 还有滚!谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-06
    • 2015-12-06
    • 2018-06-15
    • 1970-01-01
    相关资源
    最近更新 更多