第一行顶部间距UICollectioViewCell

时间:2014-05-01 15:55:52

标签: ios objective-c uicollectionview

当我有不同的UICollectionViewCell高度时,我有问题将第一行与顶部对齐,我不知道为什么。似乎minimumLineSpacingForSectionAtIndex方法无效。

这是我的结果:

enter image description here

如您所见,第一行未与顶部对齐。

这是我的代码:

- (void)viewDidLoad
{
...
FlowLayout *flowLayout = [[FlowLayout alloc] init];
flowLayout.itemSize = CGSizeMake(152, 260);
flowLayout.sectionInset = UIEdgeInsetsMake( 5, 5, 5, 5);
flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
self.collectionView.collectionViewLayout = flowLayout;
...
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:
(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)
indexPath{

if (indexPath.row == 0)
    return CGSizeMake(152, 200);
if (indexPath.row == 1)
    return CGSizeMake(152, 270);

return CGSizeMake(152, 300);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:
(UICollectionViewLayout*)collectionViewLayout    
minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {

return 5.0;

}

- (CGFloat)collectionView:(UICollectionView *)collectionView 
layout:(UICollectionViewLayout*)collectionViewLayout 
minimumLineSpacingForSectionAtIndex:(NSInteger)section {

return 8.0;

}

这是我的FlowLayout的代码:

@implementation FlowLayout

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect 
{
NSMutableArray *result = [NSMutableArray array];
NSInteger sectionCount = 1;
if ([self.collectionView.dataSource respondsToSelector:@selector(numberOfSectionsInCollectionView:)])
{
    sectionCount = [self.collectionView.dataSource numberOfSectionsInCollectionView:self.collectionView];
}
for (int s = 0; s < sectionCount; s++)
{
    NSInteger itemCount = [self.collectionView.dataSource collectionView:self.collectionView numberOfItemsInSection:s];
    for (int i = 0; i < itemCount; i++)
    {
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:s];
        UICollectionViewLayoutAttributes *layoutAttributes = 
        [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader atIndexPath:indexPath];
        if (layoutAttributes != nil) {
            [result addObject:layoutAttributes];
        }
        UICollectionViewLayoutAttributes *layout = [self layoutAttributesForItemAtIndexPath:indexPath];
        if (CGRectIntersectsRect(rect, layout.frame))
        {
            [result addObject:layout];
        }
    }
}
return result;
}


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

if (indexPath.item == 0 || indexPath.item == 1) // degenerate case 1, first item of section
    return atts;

NSIndexPath* ipPrev =
[NSIndexPath indexPathForItem:indexPath.item-2 inSection:indexPath.section];

CGRect fPrev = [self layoutAttributesForItemAtIndexPath:ipPrev].frame;
CGFloat rightPrev = fPrev.origin.y + fPrev.size.height + 10;
if (atts.frame.origin.y <= rightPrev) // degenerate case 2, first item of line
    return atts;

CGRect f = atts.frame;
f.origin.y = rightPrev;
atts.frame = f;
return atts; 
}

- (BOOL) shouldInvalidateLayoutForBoundsChange:(CGRect)newBound {
return YES;
}

1 个答案:

答案 0 :(得分:1)

使用UICollectionViewFlowLayout时,您遇到的是正常的单元格放置。

您必须调整流布局返回的布局属性。为此,请覆盖layoutAttributesForElementsInRect:方法和返回布局属性的任何方法。修改父类提供的属性,然后返回它们。您必须修改的属性是frame

这样做的一个很好的例子是来自Apple的示例代码 - Custom Layouts: A Worked Example

附加布局实施后的更新。

layoutAttributesForItemAtIndexPath:方法中,您似乎没有为第一项(索引frame)调整0。您将返回从父类获得的属性。对于项目origin.y,将框架的0更改为0