简体   繁体   English

当标题位于视图顶部时取消组合UITableView节标题

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

I am currently working on a UITableViewController. 我目前正在研究UITableViewController。 My current implementation includes a header view (white view), a UITableView section header (red view) and tableview (grey). 我当前的实现包括标题视图(白色视图),UITableView节标题(红色视图)和tableview(灰色)。 Currently the TableView style is grouped so the Section Header scrolls with the TableView when scrolling up to shrink the header view. 目前,TableView样式已分组,因此在向上滚动缩小标题视图时,Section Header会与TableView一起滚动。

I am trying to keep the Section Header at the top of the view when the header view goes offscreen, and allow the UITableViewCells to scroll under the Section Header. 当标题视图离屏时,我试图将Section Header保持在视图的顶部,并允许UITableViewCells在Section Header下滚动。 Currently the tableview cannot scroll up once the Section Header reaches the top of the view. 目前,一旦Section Header到达视图顶部,tableview就无法向上滚动。 Any suggestions would be super helpful 任何建议都会非常有帮助

在此输入图像描述

在此输入图像描述

Current Code: 现行代码:

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

I was able to replicate your desired behaviour without using the scrollViewDidScroll method, however the headerView will no longer scroll with the tableView . 我能够在不使用scrollViewDidScroll方法的情况下复制您想要的行为,但是headerView将不再使用tableView滚动。 You could use the scrollViewDidScroll method to change the height/origin of the headerView when the content offset is less than zero. 你可以使用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 }
}

故事板

I am not completely sure I follow what you are trying to accomplish, but please allow me to attempt to infer and feel free to provide clarifications. 我并不完全确定我会遵循您要完成的任务,但请允许我尝试推断并随意提供说明。 I like the table view practice :) 我喜欢桌子视图练习:)

As I understand it: 据我了解:

1) You want the white view, the red view, and the table cells beneath the red view to scroll upward from the default position 1)您希望红色视图下方的白色视图,红色视图和表格单元格从默认位置向上滚动

2) You want the white view to scroll out of visibility 2)您希望白色视图滚出可见性

3) You want the red view to float at the top while the cells scroll beneath it 3)您希望红色视图在顶部浮动,而单元格在其下方滚动

4) You currently have the white view as a table header, the red view as a section header, and the gray area are table cells 4)您当前将白色视图作为表头,红色视图作为节头,灰色区域是表格单元格

Sounds like a cool app! 听起来很酷!

Why not use 2 table sections: 为什么不使用2个表格部分:

1) Drop the table header 1)删除表头

2) Set the white view in cell 0 section 0 of the table view. 2)在表格视图的单元格0部分0中设置白色视图。

3) Set table delegate methods so section 0 will have a nil header with 0 height 3)设置表委托方法,因此第0节将有一个0高度的nil标题

3.5) Also set the table delegate methods so section 1 will be your main table 3.5)还设置表委托方法,因此第1节将是您的主表

4) Use UITableViewStylePlain so section 1 header will float at the top 4)使用UITableViewStylePlain,因此第1节标题将浮动在顶部

Is this the desired functionality? 这是所需的功能吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM