ScrollView或UICollectionView中的UItableViews?

时间:2014-01-27 12:41:25

标签: ios uitableview uiscrollview uicollectionview

我正在制作iOS应用程序,我需要显示某种水平滚动视图,其中包含一些表格视图。它必须这样:

https://dl.dropboxusercontent.com/u/53942814/Pic1.png https://dl.dropboxusercontent.com/u/53942814/Pic2.png

当用户向左或向右滑动时,将显示相应的表格视图。

我找到了两种方法:

  1. 带有UITableViews的UIScrollView
  2. 一个UICollectionView。
  3. 有没有人知道什么是最好的方式以及如何实现它?

1 个答案:

答案 0 :(得分:0)

我在我的代码中实现了完全相同的功能,可以像控制一样复制android ......

请查看我的代码的某些部分,您将获得一些想法

我通过向UIScrollView添加Views来完成它。 使用2个滚动视图,一个用于标题,一个用于表

<强> 1。将表格视图添加到表格的滚动视图

pageNo = 1;

for (Page *page in pageArray)
{
    UIStoryboard *mystoryboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
    PageContentViewController *contentViewController = [mystoryboard instantiateViewControllerWithIdentifier:@"pageContentViewController"];

    contentViewController.detailsDictionary = page.deviceDetailDictionary; //assigning dictionary will use as table data source

    /* Content page's x position calculation */

    // Formula: xPos = scrollViewWidth * ( pageNo -1 )
    // Where,
    // pageNo starts with 1

    int xPos = self.detailScrollView.frame.size.width * (pageNo -1);
    contentViewController.view.frame = CGRectMake(xPos, 0, self.detailScrollView.frame.size.width, contentViewController.view.frame.size.height);
    pageNo ++;

    [self addChildViewController:contentViewController];
    [self.detailScrollView addSubview:contentViewController.view];
    [contentViewController didMoveToParentViewController:self];
}

<强> 2。将UILabel添加到titleScrollView

只需检查此公式,即可将标题UILabel视图添加到顶部滚动视图

    // Formula: xPos = 75 + 10 * pageNo + (pageNo -1) * 150;
    // Where,
    // pageNo starts with 1
    // 75 = Left margin of first title
    // 10 = Space between two labels
    // 150 = Width of label 

第3。使用ScrollView的Delegate方法(在其中添加了表格)来处理滑动

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;
{
    CGFloat pageWidth = self.detailScrollView.frame.size.width;

    //1. Calculate page no. if 50 percent or more area of any page visible make it as current page
    int pageNo = floor((self.detailScrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 2;

    //2. Make only current page (calculated in 1.) visible by scrolling to that page in detailScrollView
    //Formula : xPos = pageWidth * (pageNo -1)
    CGRect frame = CGRectMake( pageWidth * (pageNo -1), 0, pageWidth, 50);
    [self.detailScrollView scrollRectToVisible:frame animated:YES];//OR set content offset

    //3. Make title visible of current page by scrolling to that title in titleScrollView
    //Formula : xPos = (pageWidth / 2) * (pageNo -1)
    CGRect titleFrame = CGRectMake( 160 * (pageNo -1), 0, pageWidth, 10);
    [self.titleScrollView scrollRectToVisible:titleFrame animated:YES];//OR set content offset

    //Fade effect for non current titles

    //Set all page title alpha to low value
    for (UILabel *title in pageTitleArray)
    {
        [title setAlpha:0.5];
    }

    //set current page alpha to 1
    UILabel *title = [pageTitleArray objectAtIndex:pageNo -1];
    [title setAlpha:1];
}

3中的代码负责在动画滑动时移动到下一个/上一页

希望这对你来说是一个很好的起点...