簡體   English   中英

當標題位於視圖頂部時取消組合UITableView節標題

[英]Ungrouping UITableView Section Header when Header is top of view

我目前正在研究UITableViewController。 我當前的實現包括標題視圖(白色視圖),UITableView節標題(紅色視圖)和tableview(灰色)。 目前,TableView樣式已分組,因此在向上滾動縮小標題視圖時,Section Header會與TableView一起滾動。

當標題視圖離屏時,我試圖將Section Header保持在視圖的頂部,並允許UITableViewCells在Section Header下滾動。 目前,一旦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)
    }
}
}

我能夠在不使用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 }
}

故事板

我並不完全確定我會遵循您要完成的任務,但請允許我嘗試推斷並隨意提供說明。 我喜歡桌子視圖練習:)

據我了解:

1)您希望紅色視圖下方的白色視圖,紅色視圖和表格單元格從默認位置向上滾動

2)您希望白色視圖滾出可見性

3)您希望紅色視圖在頂部浮動,而單元格在其下方滾動

4)您當前將白色視圖作為表頭,紅色視圖作為節頭,灰色區域是表格單元格

聽起來很酷!

為什么不使用2個表格部分:

1)刪除表頭

2)在表格視圖的單元格0部分0中設置白色視圖。

3)設置表委托方法,因此第0節將有一個0高度的nil標題

3.5)還設置表委托方法,因此第1節將是您的主表

4)使用UITableViewStylePlain,因此第1節標題將浮動在頂部

這是所需的功能嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM