setScrollDirection:对于CollectionView不起作用

时间:2012-11-14 16:59:26

标签: objective-c

我在ViewController中以编程方式实现了Collection View,并将其与Storyboard连接,但是滚动不起作用,一半的单元格没有出现,因为它们褪色到右边:

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.collectionView registerClass:[FotoCell class]
            forCellWithReuseIdentifier:@"cell"];

    UICollectionViewFlowLayout *myLayout = [[[UICollectionViewFlowLayout alloc]init]autorelease];
    [myLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    [self.collectionView setCollectionViewLayout:myLayout];

}

你知道为什么吗?

1 个答案:

答案 0 :(得分:1)

您需要删除viewDidLoad中的registerClass行,并在UICollectionViewDelegate的Datasource方法中设置重用标识符,如下所示 -

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:      (NSIndexPath *)indexPath
{
    FotoCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

  ....

    return cell;
}