UICollectionViewFlowLayout高度与UICollecionView高度之间的关系

时间:2014-05-16 14:22:38

标签: ios objective-c uicollectionview

我遇到一个问题,似乎我的UICollectionView认为我的屏幕较小(iPad Air),并且他无法及时加载下一个单元格,并将其加载到后期原因中#34 ;孔&#34 ;. 我有2列,动态单元格高度,并将布局子类化以获得恒定的垂直间距。

我现在可以看到,唯一可以解决这个问题的方法就是让UICollectionView加载更多单元格,这样我就可以向下滚动并且 NOT 看到一个空格,就是设置高度collectionView远高于屏幕尺寸。现在,向下滚动时,所有单元格都会及时加载。但是,现在当我向上滚动时,我得到的空格效果相同。 空表示半屏是空的,只有当我继续滚动时,他才会加载这些单元格。

我似乎暂时无法解决这个问题,但我认为设置布局与colleciontview内容大小之间的关系可能会出错。

以下是我如何设置它们:

 //collection view
    TopAlignedCollectionViewFlowLayout *layout=[[TopAlignedCollectionViewFlowLayout alloc] init];

    CGRect  size=CGRectMake( ([UIScreen mainScreen].bounds.size.width-collectionWidth)/2,
                            upperLineMargin, collectionWidth, (6.*[UIScreen mainScreen].bounds.size.height )); //seting *6 soves the problem.


    self.GridView=[[UICollectionView alloc] initWithFrame:size collectionViewLayout:layout];
    [self.GridView registerClass:[GridCell class] forCellWithReuseIdentifier:@"Cell"];
     //[ layout setItemSize:CGSizeMake(collectionWidth, [UIScreen mainScreen].bounds.size.height )];

这个问题只发生在我试图为行间的相等间距子类化布局之后。 我们现在处于这样的情况:我们没有去哪里,我们无法使用此错误的集合,因为临时空格看起来不太好。

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewLayoutAttributes* currentItemAttributes = [super layoutAttributesForItemAtIndexPath:indexPath];
      if (indexPath.item < numColumns)
    {
        CGRect f = currentItemAttributes.frame;
        f.origin.y = 0;
        currentItemAttributes.frame = f;
        return currentItemAttributes;
    }


    NSIndexPath* ipPrev = [NSIndexPath indexPathForItem:indexPath.item-numColumns inSection:indexPath.section];
    CGRect fPrev = [self layoutAttributesForItemAtIndexPath:ipPrev].frame;
    CGFloat YPointNew = fPrev.origin.y + fPrev.size.height + 10;
    CGRect f = currentItemAttributes.frame;
    f.origin.y = YPointNew;
    currentItemAttributes.frame = f;
    return currentItemAttributes;
}

0 个答案:

没有答案