jo1995
jo1995

Reputation: 414

How to make CollectionView and TableView scrollable at once?

I have a collection view obove and a table view below. What I want to do is, when I scroll down the table view, I want to take along the collection view.

This time, the collection view does not move when I scroll the table view.

Thanks in advance for your little help!

My Collection view above and tableView on the bottom

Here my CollectionView an TableView structure:

Upvotes: 2

Views: 135

Answers (3)

Vlad M.
Vlad M.

Reputation: 149

1.) You could set the tableView.tableHeaderView to be your collection view.

2.) The second solution is that can also use the UITableViewDelegate's method viewForHeaderInSection and return your collection view. Don't forget to also implement the heightForHeaderInSection method. For this to work you also have to set the table view's style to grouped. This can be done in the storyboard.

Upvotes: 0

jo_h_es
jo_h_es

Reputation: 110

If you want to scroll down the collection view put the collection view inside the table view. In this case both will be scrolled down if you scroll down the table view

Upvotes: 4

Mario
Mario

Reputation: 459

You could use the UIScrollViewDelegate's method scrollViewDidScroll(_:).

Your view controller should look like this:

extension ViewController: UIScrollViewDelegate {

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if scrollView == tableView {
            collectionView.contentOffset = scrollView.contentOffset // or f(scrollView.contentOffset)
        }
    }

}

Upvotes: 0

Related Questions