无法滚动UICollectionViewController

时间:2014-03-31 15:37:53

标签: ios objective-c uicollectionview

我正在尝试学习如何使用UICollectionViewController。使用下面的代码,我无法滚动到底部的单元格行。

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    ViewController *viewController = [[ ViewController alloc] initWithCollectionViewLayout:[self flowLayout]];
    self.window = [[ UIWindow alloc] initWithFrame: [[ UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    self.window.rootViewController = viewController;
    [self.window makeKeyAndVisible];

    return YES;
}

- (UICollectionViewFlowLayout *) flowLayout
{
    UICollectionViewFlowLayout *flowLayout = [[ UICollectionViewFlowLayout alloc] init];
    flowLayout.minimumLineSpacing = 20.0f;
    flowLayout.minimumInteritemSpacing = 10.0f;
    flowLayout.itemSize = CGSizeMake( 80.0f, 120.0f);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.sectionInset = UIEdgeInsetsMake( 10.0f, 20.0f, 10.0f, 20.0f);
    return flowLayout;
}

ViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.scrollEnabled = YES;
    self.collectionView.alwaysBounceVertical = YES;
}

来自ViewController.m的附加代码;请求了附加代码。

- (UICollectionViewCell *) collectionView:( UICollectionView *) collectionView
  cellForItemAtIndexPath:( NSIndexPath *) indexPath
{
    UICollectionViewCell *cell = [collectionView 
            dequeueReusableCellWithReuseIdentifier:kCollectionViewCellIdentifier 
                                      forIndexPath:indexPath];
    cell.backgroundColor = [self allSectionColors][ indexPath.section];
    return cell;
}

- (NSArray *) allSectionColors
{
    static NSArray *allSectionColors = nil;
    if (allSectionColors == nil)
    {
        allSectionColors = @[ [UIColor redColor],
                            [UIColor greenColor],
                            [UIColor blueColor],
                            ];
    }
    return allSectionColors;
}

- (NSInteger) numberOfSectionsInCollectionView :( UICollectionView *) collectionView
{
    return [self allSectionColors].count;
}

2 个答案:

答案 0 :(得分:0)

如果没有关于集合所呈现的模型的详细信息,则很难得出有关您的问题的任何结论。两个指针是,首先你将滚动方向设置为水平,然后第二个,如果单元格没有以任何方式溢出collectionView,那么后者将无法滚动。

答案 1 :(得分:0)

您无法滚动到底部的单元格行,因为您将滚动方向设置为水平:

flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

改为使用UICollectionViewScrollDirectionVertical