0

我有一个UISplitView类似的设计(不是真的UISplitView,我曾经View container模仿它)。

左侧容器有一个table view,右侧容器有一个collection view

如果我单击tableview cell左侧容器中的 ,右侧collection view将相应更改。

是分页的collection view,它总是有两页。

如果我滚动到第二页,collection view现在我table view cell在左侧容器中单击一个新的,它将collection view在右侧容器中加载正确的,但它在第二页而不是第一页!

我四处寻找,找不到解决方案。

我很感激任何建议。


更新:添加了相关代码。

以下代码在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];

}

函数setup定义如下:

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

谢谢。

4

1 回答 1

0

为什么当用户在左侧容器中选择一行时,您不能编写代码来滚动滚动视图以显示第一页的框架。

[contentScrollView scrollRectToVisible:pageRect animated:YES];

以下是我为解决此问题而创建的示例:

您可以在这里下载代码表格

下载源代码后,只需将以下方法添加到 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];
}

通过传递页面索引从 MLKPageViewController 的“ViewDidLoad”方法调用上述方法。

于 2014-01-30T08:05:22.233 回答