Enable UITableView scrolling during a scroll in UIScrollView

て烟熏妆下的殇ゞ 提交于 2019-12-13 03:46:22

问题


I have a UITableView inside a UIScrollView

The table view looks like:

private(set) lazy var tableView: UITableView = {
    let tableView = UITableView(frame: CGRect.zero, style: .plain)
    tableView.separatorStyle = .none
    tableView.showsVerticalScrollIndicator = false
    tableView.rowHeight = 70
    tableView.backgroundColor = .blue
    tableView.bounces = false
    tableView.alwaysBounceVertical = false
    tableView.isScrollEnabled = false
    return tableView
}()

As you see I have disabled the scrolling in the table view because I want it to scroll along with the rest of the views inside the UIScrollView. What I want to achieve though, is to enable the scrolling of the tableView when the UIScrollView has reached certain offset, therefore I have that in the scrollViewDidScroll delegate method:

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if scrollView.contentOffset.y >= 160 {
        self.view.tableView.isScrollEnabled = true
    }
}

The above works partially. The UITableView that just had it's scrolling enabled doesn't scroll, unless I lift my finger off the screen and start scrolling again.

Any ideas how can I get the desired behaviour ?


回答1:


Important: You should not embed UIWebView or UITableView objects in UIScrollView objects. If you do so, unexpected behaviour can result because touch events for the two objects can be mixed up and wrongly handled.



来源:https://stackoverflow.com/questions/45837882/enable-uitableview-scrolling-during-a-scroll-in-uiscrollview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!