2

我正在使用 UICollectionView 来显示彩票号码。我希望 collectionView 的单元格在自定义间距之后开始,并且在该间距中我想显示一个标题或某种自定义 UIView。

现在我的收藏视图如下所示:123 456 789

我希望它看起来像这样:

查看12 345678 9.....

我正在考虑使用自定义布局,但 UICollectionViewFlowLayout 符合我的目的。

这个问题有什么解决办法吗?

4

1 回答 1

2

解决了。

因为我只需要调整布局,就一点点,我决定使用 UICollectionViewFlowLayout 的子类。这样,我可以随心所欲地“玩”布局,更改页眉/页脚的框架等。

我的解决方案是这样的:

。H

@interface MyUICollectionViewFlowLayout : UICollectionViewFlowLayout

@end

.m

#define HEADER_VIEW_WIDTH 132
#define HEADER_VIEW_HEIGHT 44

@interface MyUICollectionViewFlowLayout ()

@end

@implementation MyUICollectionViewFlowLayout

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
    NSMutableArray *allAttributes = [[super layoutAttributesForElementsInRect:rect] mutableCopy];

    for (UICollectionViewLayoutAttributes *attribute in allAttributes) {

// if it's a cell, change it's layout to be right after the Header, and in the same line.
        if (attribute.representedElementCategory == UICollectionElementCategoryCell) {

            switch (attribute.indexPath.row) {
                case 0:
                    attribute.frame = CGRectMake(HEADER_VIEW_WIDTH, 0, attribute.frame.size.width, attribute.frame.size.height);
                    break;
                case 1:
                    attribute.frame = CGRectMake(HEADER_VIEW_WIDTH + attribute.frame.size.width , 0, attribute.frame.size.width, attribute.frame.size.height);
                    break;
                case 2:
                    attribute.frame = CGRectMake(HEADER_VIEW_WIDTH + attribute.frame.size.width * 2 , 0, attribute.frame.size.width, attribute.frame.size.height);
                    break;
                case 3:
                    attribute.frame = CGRectMake(HEADER_VIEW_WIDTH + attribute.frame.size.width * 3 , 0, attribute.frame.size.width, attribute.frame.size.height);
                    break;
                case 4:
                    attribute.frame = CGRectMake(HEADER_VIEW_WIDTH + attribute.frame.size.width * 4 , 0, attribute.frame.size.width, attribute.frame.size.height);
                    break;
                case 5:
                    attribute.frame = CGRectMake(HEADER_VIEW_WIDTH + attribute.frame.size.width * 5 , 0, attribute.frame.size.width, attribute.frame.size.height);
                    break;
                case 6:
                    attribute.frame = CGRectMake(HEADER_VIEW_WIDTH + attribute.frame.size.width * 6 , 0, attribute.frame.size.width, attribute.frame.size.height);
                    break;
            // if it's a header view, move it to the same line as all the cells.
        } else if (attribute.representedElementCategory == UICollectionElementCategorySupplementaryView){

            attribute.frame = CGRectMake(0, 0 , HEADER_VIEW_WIDTH, HEADER_VIEW_HEIGHT);
        }
    }
    return allAttributes;
}



@end

我想我可以编写一些算法而不是 switch/case,但我更愿意将优化问题留到以后。

于 2013-10-20T12:20:26.453 回答