首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UICollectionViewCell不绘制indexpath.row == 0上的单元格

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

Stack Overflow用户
提问于 2014-05-19 01:40:26
回答 1查看 151关注 0票数 0

请参阅附呈的图片。

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

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

它似乎是一个iOS SDK错误,但需要进一步研究。只是想知道有没有人想试试这个。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-19 01:59:42

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

UICollectionView flowLayout not wrapping cells correctly (iOS)1

但是我的情况是唯一的,因为我使用一个UIDynamicAnimator来计算布局属性。

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

代码语言:javascript
复制
-(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];
}

对此:

代码语言:javascript
复制
-(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代码中的逻辑做错了一些事情,这是可有可无的,但我已经检查过了,逻辑看起来还不错。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23728619

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档