2016-10-01 89 views
3

我目前正在使用UITableViewController。我目前的实现包括一个标题视图(白色视图),一个UITableView部分标题(红色视图)和tableview(灰色)。当前TableView样式被分组,所以当滚动缩小标题视图时,Section Header与TableView一起滚动。当头部位于视图顶部时取消组合UITableView部分头部

我试图保持部分标题在视图的顶部,当标题视图离屏时,并允许UITableViewCells在部分标题下滚动。目前,当Section Header到达视图顶部时,tableview不能向上滚动。任何建议将是超级有用

enter image description here

enter image description here

目前代码:

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) 
    } 
} 
} 
+1

你为什么不使用['plain'](https://developer.apple。com/reference/uikit/uitableviewstyle/1614968-plain)table view改为?标题不会浮动在分组表格视图中。 – ozgur

+1

要实现这个功能,你应该使用UIViewController。你可以在UIViewController中轻松实现这一点,为什么使用UITableViewController复杂化。 – Priyansh

+0

当它在视图中间时,普通表格视图不会滚动SectionHeader。它仍然在同一个地方 –

回答

2

我不能完全肯定我跟着你想什么来完成,但请允许我试图推断并随时作出澄清。我喜欢表视图做法:)

据我了解:

1)你想白认为,鉴于红色,红色视图下方的表格单元格从默认位置向上滚动

2)你想白色视图滚动出能见度

3)要红色视图漂浮在顶部,而细胞滚动它下面

4)您目前的白色视图作为表头,红色视图作为一个se ction标题和灰色区域是表单元格

听起来很酷的应用程序!

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

1)删除表头

2)设置在小区0表视图的部分0的白色视图。

3)设置表委托方法,以便部分0将具有零标头0高度

3.5)亦设置表委托方法如此部1将是您的主表

4)使用UITableViewStylePlain所以第1节标题将浮动在顶部

这是所需的功能吗?

+0

这是一个非常整洁的实现。我会尝试一下 –

+0

让我知道它是如何工作的,如果你喜欢它,随时接受:) – Lytic

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 } 
} 

storyboard

+0

林不知道这个解决方案是如何是理想的行为。标题不停留在视图的顶部,但是不在屏幕上 –

+0

您是否需要将白色标题作为'tableViewHeader',或者它可以仅仅是视图控制器中的'@ IBOutlet'? – Callam

+0

是的,它不一定是一个头。任何视图都会做 –