【问题标题】:Ungrouping UITableView Section Header when Header is top of view当标题位于视图顶部时取消组合 UITableView 部分标题
【发布时间】:2017-02-09 16:40:12
【问题描述】:

我目前正在开发 UITableViewController。我当前的实现包括一个标题视图(白色视图)、一个 UITableView 部分标题(红色视图)和表格视图(灰色)。目前 TableView 样式是分组的,因此当向上滚动以缩小标题视图时,Section Header 会随 TableView 一起滚动。

当标题视图离开屏幕时,我试图将部分标题保留在视图顶部,并允许 UITableViewCells 在部分标题下滚动。目前,一旦 Section Header 到达视图顶部,tableview 就无法向上滚动。任何建议都会非常有帮助

当前代码:

    override func tableView(_ tableView: UITableView, heightForHeaderInSection     section: Int) -> CGFloat {
    return 106.0
}

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let view1 = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 106.0))
    view1.backgroundColor = UIColor.red
    return view1
}

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return  80.0
}

override func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let maxOffset = (scrollView.contentSize.height - scrollView.frame.size.height)
    if scrollView.contentOffset.y >=  maxOffset {
        scrollView.contentOffset = CGPoint(x: scrollView.contentOffset.x, y: maxOffset)
    }

    if scrollView.contentOffset.y >= 35.0 {
        scrollView.contentOffset = CGPoint(x: scrollView.contentOffset.x, y: 35.0)
    }
}
}

【问题讨论】:

  • 你为什么不使用plain 表格视图呢?标题不会在分组表视图中浮动。
  • 要实现这个功能,你应该使用 UIViewController。你可以在 UIViewController 中轻松实现这一点,为什么使用 UITableViewController 会使事情复杂化。
  • 普通表格视图在 SectionHeader 位于视图中间时不会滚动它。它仍然在同一个地方
  • @Priyansh 这与在 UITableViewController 上使用 UIViewController 无关。 OP 只希望部分标题在表格视图滚动时浮动,这可以通过 .plain 表格视图轻松完成。
  • @ozgur 我正在寻找标题部分,仅当部分标题位于视图顶部时才浮动。 .plain 表视图没有实现这一点

标签: ios iphone swift uitableview uiscrollview


【解决方案1】:

我不完全确定我是否遵循您要完成的任务,但请允许我尝试推断并随时提供澄清。我喜欢表格视图的做法:)

据我了解:

1) 您希望白色视图、红色视图和红色视图下方的表格单元格从默认位置向上滚动

2) 您希望白色视图滚动到可见性之外

3) 您希望红色视图浮动在顶部,而单元格在其下方滚动

4) 您当前将白色视图作为表头,红色视图作为节头,灰色区域是表格单元格

听起来很酷的应用程序!

为什么不使用 2 个表格部分:

1) 删除表头

2) 在表格视图的单元格 0 部分 0 中设置白色视图。

3) 设置表委托方法,因此第 0 节将有一个高度为 0 的 nil 标题

3.5) 还要设置表委托方法,以便第 1 部分成为您的主表

4) 使用 UITableViewStylePlain 使第 1 部分的标题将浮动在顶部

这是想要的功能吗?

【讨论】:

  • 这是一个非常简洁的实现。我去试试
  • 告诉我它是如何工作的,如果你喜欢,请随时接受:)
【解决方案2】:

我能够在不使用scrollViewDidScroll 方法的情况下复制您想要的行为,但是headerView 将不再使用tableView 滚动。当内容偏移量小于零时,您可以使用scrollViewDidScroll 方法更改headerView 的高度/原点。

class TableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet weak var headerView: UIView?
    @IBOutlet weak var tableView: UITableView? {
        didSet {
            tableView?.dataSource = self
            tableView?.delegate = self
        }
    }

    func numberOfSections(in tableView: UITableView) -> Int { return 1 }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 10 }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = UITableViewCell(style: .default, reuseIdentifier: "Cell")

        cell.backgroundColor = UIColor.gray;

        return cell
    }

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

        let header = UIView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: 50))

        header.backgroundColor = UIColor.red

        return header
    }

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { return 100 }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 60 }
}

【讨论】:

  • 我不确定这个解决方案是怎样的行为。标题不会停留在视图顶部,而是离开屏幕
  • 您要求白色标题是tableViewHeader 还是可以只是视图控制器中的@IBOutlet
  • 是的,它不必是标题。任何视图都可以
  • 你在使用导航控制器吗?
  • 没有导航控制器。
猜你喜欢
  • 2017-07-14
  • 1970-01-01
  • 2014-07-06
  • 1970-01-01
  • 1970-01-01
  • 2014-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多