【问题标题】:Hide tableview header when the numberOfRows in section = 0当 section 中的 numberOfRows = 0 时隐藏 tableview 标题
【发布时间】:2017-03-17 23:54:29
【问题描述】:

如果该部分没有返回任何行,我目前正在尝试隐藏 tableview 标题。这是我目前拥有的,但它在运行时返回 EXC_BAD_ACCESS 错误:

  func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    let headerHeight: CGFloat

    switch section {
    case 0:
      headerHeight = 0
    case 1:
      if peopleImagesTableView.numberOfRows(inSection: 1) == 0 {
          headerHeight = 0
      } else {
        headerHeight = 40
      }
    default:
      headerHeight = 0
    }
    return headerHeight
  }

【问题讨论】:

  • 你需要返回浮点值。
  • 虽然代码行导致错误?
  • @rmaddy 很抱歉回答这个问题。:d
  • @TusharSharma 问题是 OP 已经返回了一个浮点值。
  • @rmaddy 变量是 CGFloat 类型,但他的返回值为 40 或 0。可以吗??

标签: ios swift uitableview tableview


【解决方案1】:

我认为问题出在if peopleImagesTableView.numberOfRows(inSection: 1) == 0 { 这一行。因为tableView(_:heightForHeaderInSection:)tableView(_:numberOfRowsInSection:) 之前运行,所以它无法知道行数和应用程序崩溃。您应该检查您的数据数组,而不是调用 numberOfRows 函数。例如:

if data[1].count == 0 {
    headerHeight = 0
} else {
    headerHeight = 40
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-21
    • 2015-01-22
    • 1970-01-01
    • 2018-08-10
    • 1970-01-01
    • 2011-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多