简体   繁体   中英

Scroll Multiple UICollectionView with one collectionview IOS

I am working on an application in which, I have UITableView and in each cell of uitableview i have uicollectionview scrolling horizontally.

Now, What I want is that: When I scroll one collectionview in any direction then all other collectionviews of my table should scroll accordingly in that direction.

I have tried using scrollview delegates but it is not working properly.

I am stuck in this issue. I have looked on internet but could not found anything yet.

Help needed! Thanks

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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