0

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.

user890207
  • 509
  • 1
  • 6
  • 14
  • Where are you setting contentsize for collectionView? – Naga Mallesh Maddali Jan 31 '14 at 05:07
  • I did not set contentsize. It uses the default one. I have figured out the problem. You are correct that I should use `[contentScrollView scrollRectToVisible:pageRect animated:YES];` to set the location. But I should NOT put it in ViewDidLoad. Since this is a delegate from the `left container`, I should put it in the `delegate` function. Then It works. Thanks! – user890207 Jan 31 '14 at 05:27
  • Great that you got it. Actually, I have created a example for you. I am about to post it. But its good that you are able to resolve it. – Naga Mallesh Maddali Jan 31 '14 at 05:29
  • It would be great if you can post your code. I am sure it will help me and others. :-) – user890207 Jan 31 '14 at 06:00
  • I have edited my answer with example code and explanation. Please up vote it if it helps you. – Naga Mallesh Maddali Jan 31 '14 at 07:14

1 Answers1

0

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.

Naga Mallesh Maddali
  • 1,005
  • 10
  • 19