UICollectionViewFlowLayout子类不起作用

时间:2014-04-12 19:17:10

标签: ios uicollectionview uicollectionviewlayout

这是我的UICollectionViewFlowLayout子类:

@implementation MyCollectionViewFlowLayout

- (id)init
{
    self = [super init];
    if (self) {
        [self setup];
    }

    return self;
}

- (void)setup
{
    self.itemSize = CGSizeMake(320, 320);
    self.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.minimumInteritemSpacing = 0;
    self.minimumLineSpacing = 0;
    self.scrollDirection = UICollectionViewScrollDirectionVertical;
}


- (void)prepareLayout {
    [super prepareLayout];

}

- (CGSize)collectionViewContentSize {
    return self.itemSize;
}

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
    NSArray* allAttributesInRect = [super layoutAttributesForElementsInRect:rect];

    return allAttributesInRect;
}


- (UICollectionViewLayoutAttributes*)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewLayoutAttributes* attributes = [super layoutAttributesForItemAtIndexPath:indexPath];

    return attributes;
}

@end

当我执行以下操作时,集合视图将不会滚动:

MyCollectionViewFlowLayout* flowLayout = [[MyCollectionViewFlowLayout alloc] init];

但是,如果我这样做:

UICollectionViewFlowLayout* flowLayout = [[UICollectionViewFlowLayout alloc] init];
flowLayout.itemSize = CGSizeMake(320, 320);
flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
flowLayout.minimumInteritemSpacing = 0;
flowLayout.minimumLineSpacing = 0;
flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;

然后集合视图滚动得很好。我在UICollectionViewFlowLayout子类中做错了什么?

1 个答案:

答案 0 :(得分:0)

问题是collectionViewContentSize

- (CGSize)collectionViewContentSize {
    return self.itemSize;
}

这里返回itemSize意味着集合视图的整个contentSize只是一个项目的大小。尝试删除该代码,或将其更改为

- (CGSize)collectionViewContentSize {
    [super collectionViewContentSize];
}