Scroll Multiple UICollectionView with one collectionview IOS

喜你入骨 提交于 2019-12-04 16:07:29

Ok I've checked my code, what I do was: I got a tableView on the left side of the screen, and on the rest a UIScrollView, inside it I had on the top a view which width is equal to the collectionView.contentSize.width, and below that view, the collectionView, with its height equal to the screen height, and its width equal to its contentSize.width. After that, the ScrollView only scrolls horizontally, and the collectionView only scrolls vertically, so, when you scroll horizontally, the tableView stays, and the header view and the collection scrolls horizontally, and if you scroll vertically the header view stays fix, and the collection view and the tableView scrolls at the same time (you have to link their delegates).

That's what I do in the UIScrollViewDelegate

pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == _scrollView) {
        if (scrollView.contentOffset.y > 0  ||  scrollView.contentOffset.y < 0 ) {
            scrollView.contentOffset = CGPointMake(scrollView.contentOffset.x, 0);
        }
    } else {
        _tableView.contentOffset = CGPointMake(0, scrollView.contentOffset.y);
        _collectionView.contentOffset = CGPointMake(0, scrollView.contentOffset.y);
    }
}

The height of the cells of the tableView and the height of the cells of the collectionView was the same.

You can coordinate two tableviews by declaring the containing view controller as a UITableViewDelegate and implementing the scrollView delegate method:

// say tv0 and tv1 are outlets to two table views
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == self.tv0) {
        self.tv1.contentOffset = self.tv0.contentOffset;
    } else if (scrollView == self.tv1) {
        self.tv0.contentOffset = self.tv1.contentOffset;
    }
}

To get the hang of it this first with a couple simple table views with the same length content. You'll need to add conditional logic to handle when one view has a greater contentSize than the other.

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