Подкласс UICollectionViewFlowLayout не работает

Вот мой подкласс 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?


person soleil    schedule 12.04.2014    source источник


Ответы (1)


Проблема в collectionViewContentSize

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

Возврат itemSize здесь означает, что весь contentSize для вашего представления коллекции равен только размеру одного элемента. Попробуйте удалить этот код или изменить его на

- (CGSize)collectionViewContentSize {
    [super collectionViewContentSize];
}
person Alej    schedule 12.04.2014
comment
Вам даже не нужно их реализовывать, суперкласс должен реализовать это для макета потока. - person barndog; 23.06.2014