【问题标题】:UITableView section header views disappear when scrollingUITableView 部分标题视图在滚动时消失
【发布时间】:2017-07-14 06:39:09
【问题描述】:

我目前在 UITableView 中使用了一些自定义部分标题视图。视图在加载 UITableView 时出现,但在滚动时消失。我看到了这篇文章,但它似乎已经过时了:tableView section headers disappear SWIFT 这是我的标题视图代码:

  func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

      let view = UIView()

      switch section {
      case 0: break

      default:

        view.backgroundColor = UIColor.clear
        let image = UIImageView(image:"Line")
        image.frame = CGRect(x: 8, y: 35, width: 340, height: 1)
        view.addSubview(image)

        let label = UILabel()
        let sectionText = self.sectionTitles[section]
        label.text = sectionText
        label.textColor = UIColor.white
        label.font = UIFont(name:"Helvetica Neue" , size: 17)
        label.frame = CGRect(x: 10, y: 8, width: 200, height: 20)
        view.addSubview(label)
        }

      return view
  }


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

    switch section {
    case 0:
      // hide the first section header
      headerHeight = CGFloat.leastNonzeroMagnitude
    default:
      headerHeight = 40
    }

    return headerHeight
  }

【问题讨论】:

  • 可能不相关,但为什么不为第 0 部分的标题高度返回 0?为什么要返回非常小的、几乎为 0 的值而不是 0?
  • 我试过了,真的没什么区别
  • 我还建议使用更有用的非零初始帧创建标题视图。

标签: ios swift uitableview


【解决方案1】:

仅当表格的 UITableViewStyle 属性设置为 UITableViewStylePlain 时,标题才会保持固定。如果您将其设置为 UITableViewStyleGrouped,标题将与单元格一起向上滚动。

因此,如果您想修复会粘在顶部的标题,则需要将 UITableViewStyle 设置为 UITableViewStylePlain

如果您想向上滚动单元格,则需要将 UITableViewStyle 设置为 UITableViewStyleGrouped

【讨论】:

  • 我的是 UITableViewStylePlain,滚动一行后标题消失。
猜你喜欢
  • 2012-04-05
  • 1970-01-01
  • 2017-02-09
  • 1970-01-01
  • 2013-04-26
  • 2015-03-11
  • 2015-02-18
  • 1970-01-01
相关资源
最近更新 更多