UICollectionViewCell不在indexpath.row == 0上绘制单元格

时间:2014-05-19 01:40:26

标签: ios cocoa-touch uikit uicollectionview uicollectionviewcell

查看附件中的图片。UICollectionViewCell missing

我正在使用一个相当基本的UIFlowLayout子类来跟随视图,但无论我尝试过什么,它都决定它不需要绘制单元格0,

但是,如果我向下滚动并向后滚动,它会正确地绘制它。

似乎这是一个iOS SDK错误,但需要进一步调查。只是想知道是否有人知道任何想法尝试。

1 个答案:

答案 0 :(得分:0)

这个答案实际上有助于解决它:

UICollectionView flowLayout not wrapping cells correctly (iOS) 1

但我的情况很独特,因为我使用UIDynamicAnimator来计算布局属性。

所以我的代码就是这样的:

-(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
    return [self.dynamicAnimator itemsInRect:rect];
}

-(UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return [self.dynamicAnimator layoutAttributesForCellAtIndexPath:indexPath];
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    return [self.dynamicAnimator layoutAttributesForSupplementaryViewOfKind: kind atIndexPath:indexPath];
}

对此:

-(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
    NSArray *attributes = [super layoutAttributesForElementsInRect:rect];
    NSMutableArray *newAttributes = [NSMutableArray arrayWithCapacity:attributes.count];
    for (UICollectionViewLayoutAttributes *attribute in attributes) {
        if ((attribute.frame.origin.x + attribute.frame.size.width <= self.collectionViewContentSize.width) &&
            (attribute.frame.origin.y + attribute.frame.size.height <= self.collectionViewContentSize.height)) {
            [newAttributes addObject:attribute];
        }
    }
    return newAttributes;
}

-(UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return [self.dynamicAnimator layoutAttributesForCellAtIndexPath:indexPath];
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    return [self.dynamicAnimator layoutAttributesForSupplementaryViewOfKind: kind atIndexPath:indexPath];
}

并解决了问题!我的代码中的逻辑可以说是合理的 - (BOOL)shouldInvalidateLayoutForBoundsChange做错了什么,但我已经检查过了,逻辑看起来很好。