2016-10-01 88 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

是的,它不一定是一個頭。任何視圖都會做 –