I have a UISplitView like design (not really UISplitView, I used View container to mimic it).

The left side container has a table view and right side container has a collection view.

If I click the tableview cell in the left container, the right side collection view will be changed accordingly.

The collection view is paginated, it always has two pages.

If I scroll to the second page of the collection view and now I click a new table view cell in the left container, it will load the correct collection view in the right container, but it is in the second page instead of the first page!

I search around and could not find a solution.

I appreciate for any advices.


Update: Added related code.

Following code is in the right container.

- (void)viewDidLoad
{
[super viewDidLoad];

[self setup];
self.collectionView.delegate = self;

self.pageControl.currentPage = 0;
[self.collectionView scrollRectToVisible:CGRectMake(0, 0, self.collectionView.frame.size.width, self.collectionView.frame.size.width) animated:NO];

}

The function setup is defined as following:

-(void)setup
{    
self.collectionView.pagingEnabled = YES;
self.collectionView.directionalLockEnabled = YES;
self.collectionView.bounces = NO;
self.collectionView.showsHorizontalScrollIndicator = NO;
}

Thanks.

有帮助吗?

解决方案

Why can't you write a code to scroll the scroll view to show the frame of the first page whenever user selects a row in left container.

[contentScrollView scrollRectToVisible:pageRect animated:YES];

Following is the example I have created to solve this issue :

You can download code form Here

After downloading source just add following method to MLKPageViewController.m

- (void)showPageAtIndex:(NSInteger)index
{
    if( index >= self.contentVCs.count )
        return;

    UIView *contentView = ((UIViewController *)[self.contentVCs objectAtIndex:index]).view;
    CGRect pageRect;

    if( mlkPageControl.currentPage == FIRST_PAGE || mlkPageControl.currentPage == LAST_PAGE )
    {
        pageRect = CGRectMake( (index * CONTENT_VIEW_SPACING) + index *  contentView.frame.size.width, contentScrollView.frame.origin.y , contentScrollView.frame.size.width, contentScrollView.frame.size.height);
    }
    else
    {
        pageRect = CGRectMake( (index * CONTENT_VIEW_SPACING) + index *  contentView.frame.size.width - CONTENT_VIEW_SPACING, contentScrollView.frame.origin.y , contentScrollView.frame.size.width, contentScrollView.frame.size.height);
    }

    [contentScrollView scrollRectToVisible:pageRect animated:YES];
}

Call above method from "ViewDidLoad" method of MLKPageViewController by passing page index.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top