【问题标题】:Set row height for only one tableView and ignore other tableView's in heightForRowAtIndexPath?只为一个tableView设置行高并忽略heightForRowAtIndexPath中的其他tableView?
【发布时间】:2015-09-03 01:49:57
【问题描述】:

我有一个 TableViewController 和一个 TableView,它的行通过 auto layout 动态设置。

我已将另一个 TableView 添加到用于菜单的控制器中。它的行高需要设置为 65。

问题在于,heightForRowAtIndexPath。我知道如何只为这个 tableView 菜单的行返回 65。但我不确定如何让 auto layout 为另一个 tableView 做这件事。

有谁知道我如何做到这一点?

这是我的代码:

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if tableView is YALContextMenuTableView {
        return 65
    } else {

    }
}

【问题讨论】:

    标签: ios swift uitableview heightforrowatindexpath


    【解决方案1】:

    你可以使用super来使用UITableViewController实现的这个方法:

    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        if tableView is YALContextMenuTableView {
            return 65
        } else {
            return super.tableView(tableView, heightForRowAtIndexPath:indexPath)
        }
    }
    

    【讨论】:

      【解决方案2】:
      • 不要使用tableView:heightForRowAtIndexPath:。为您的菜单 tableView 单元格调整自动布局和自动调整大小,使其约束调整其高度(到 65),或者

      • 继续使用tableView:heightForRowAtIndexPath:。为您的非菜单 tableView 单元格返回 UITableViewAutomaticDimension 的高度,这将让系统自行调整这些单元格的大小。

      自行调整大小将是首选方式,因为它与动态类型和自适应 UI 结合使用效果很好。

      【讨论】:

      • 返回 UITableViewAutomaticDimension 似乎对我有用,非常感谢☺️
      猜你喜欢
      • 2012-09-23
      • 2016-04-21
      • 2016-10-19
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 2014-12-23
      • 1970-01-01
      • 2021-12-23
      相关资源
      最近更新 更多