Enable UITableView scrolling during a scroll in UI

2019-09-14 19:32发布

问题:

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.