UICollectionView cell starting margin

元气小坏坏 提交于 2019-12-11 02:23:27

问题


I'm using a UICollectionView to display lottery numbers. I want the cells of the collectionView to start after a custom spacing, and in that spacing i would like to display a Header, or a custom UIView of some kind.

now my collection view looks like this: 123 456 789

i would like it to look like this:

view12 345678 9.....

i'm thinking about using a custom layout, but the UICollectionViewFlowLayout serves my purpose.

is there any solution for this problem?


回答1:


Solved.

since i only had to tweak the layout, just for a little bit, i decided to use a subclass of UICollectionViewFlowLayout. this way, i can "play" with the layout as i wish, change the frame of headers/footers etc.

my solution works like this:

.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

i guess that instead of switch/case, i could have written some algorithm, but i prefer to leave optimization issues for later.



来源:https://stackoverflow.com/questions/19474667/uicollectionview-cell-starting-margin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!