UICollectionView自定义布局

CustomLayout

#import 

@interface CustomLayout : UICollectionViewFlowLayout

@end

#import "CustomLayout.h"
#define kScreenWidth       ([UIScreen mainScreen].bounds.size.width)
static CGFloat const lineWidth = 1.0f;

@interface CustomLayout ()

@property (nonatomic,strong) NSMutableArray *attributesArray;

@property (nonatomic,assign) CGFloat y;

@end

@implementation CustomLayout

- (void)prepareLayout{
    [super prepareLayout];
    self.attributesArray = [NSMutableArray array];
    for (int i = 0; i < [self.collectionView numberOfSections]; i++) {
        for (int j = 0; j < [self.collectionView numberOfItemsInSection:i]; j++) {
            [self.attributesArray addObject:[self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:j inSection:i]]];
        }
        //footer的UICollectionViewLayoutAttributes
        UICollectionViewLayoutAttributes *attribute = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionFooter atIndexPath:[NSIndexPath indexPathForItem:0 inSection:i]];
        [self.attributesArray addObject:attribute];
    }
    
}

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect{
    return self.attributesArray;
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewLayoutAttributes *attribute = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    CGFloat width = kScreenWidth/3.0;
    CGFloat width1 = (kScreenWidth-lineWidth*2)/3.0;
    CGFloat width2 = (kScreenWidth-lineWidth*3)/4.0;
    if (indexPath.section == 0) {
        attribute.frame = CGRectMake(indexPath.item*width, self.y, width, width);
        NSInteger count = [self.collectionView numberOfItemsInSection:indexPath.section];
        if (indexPath.item == count-1) {
            self.y = self.y + width + 10;
        }
        
    }else if (indexPath.section == 1){
        
        if (indexPath.item == 0) { // 4
            attribute.frame = CGRectMake(0, _y, width1, width*2+lineWidth);
        } else if (indexPath.item == 1) {
            attribute.frame = CGRectMake(width1+lineWidth, _y, width1*2, width);
        } else if (indexPath.item == 2) {
            attribute.frame = CGRectMake(width1+lineWidth, width+lineWidth+_y, width1, width);
        } else if (indexPath.item == 3) {
            attribute.frame = CGRectMake((width1+lineWidth)*2, width+lineWidth+_y, width1, width);
        }
        NSInteger count = [self.collectionView numberOfItemsInSection:indexPath.section];
        if (indexPath.item == count-1) {
            self.y = self.y +  width*2+lineWidth + 10;
        }
    }else if (indexPath.section == 2){

        if (indexPath.item < 4) {
            attribute.frame = CGRectMake((width2+lineWidth)*indexPath.item, _y, width2, width);
        } else if (indexPath.item >= 4 && indexPath.item <7) {
            attribute.frame = CGRectMake((width1+lineWidth)*(indexPath.item-4), width+lineWidth+_y, width1, width);
        }
        NSInteger count = [self.collectionView numberOfItemsInSection:indexPath.section];
        if (indexPath.item == count-1) {
            self.y = self.y +  width*2+lineWidth + 10;
        }
        
    }
    else if (indexPath.section == 3){
        NSInteger row = indexPath.item/3;
        NSInteger col = indexPath.item%3;
        attribute.frame = CGRectMake((width1+lineWidth)*col, (width+lineWidth)*row+_y, width1, width);
        NSInteger count = [self.collectionView numberOfItemsInSection:indexPath.section];
        if (indexPath.item == count-1) {
            NSInteger rows = 0;
            if (count%3 == 0) {
                rows = count/3;
            }else{
                rows = count/3 + 1;
            }
            self.y = self.y +  rows * width + (rows-1)*1 + 10;
        }
    }
    
    return attribute;
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewLayoutAttributes *attribute = [UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:elementKind withIndexPath:indexPath];
    attribute.frame = CGRectMake(0, _y, kScreenWidth, 60);
    _y += 60+10;
    return attribute;
}

- (CGSize)collectionViewContentSize
{
    CGSize size = self.collectionView.bounds.size;
    
    size.height = self.y;
    
    return size;
}

@end

用法

#import "ViewController.h"
#import "CustomLayout.h"

#define random(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:(a)/255.0]
#define randomColor random(arc4random_uniform(256), arc4random_uniform(256), arc4random_uniform(256), 255)

@interface ViewController ()

@property (nonatomic,strong) UICollectionView *collectionView;
@property (nonatomic,strong) NSArray *dataArray;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.dataArray = @[
                       @[@"1",@"2",@"3"],
                       @[@"4",@"5",@"6",@"7"],
                       @[@"8",@"9",@"10",@"11",@"12",@"13",@"14"],
                       @[@"15",@"16",@"17",@"18",@"19",@"20",@"21",@"22",@"22",@"22",@"22"]
                       ];
    

    CustomLayout *layout = [[CustomLayout alloc]init];
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) collectionViewLayout:layout];
    self.collectionView.backgroundColor = [UIColor lightGrayColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.view addSubview:self.collectionView];
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"id"];
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];
    
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return  self.dataArray.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    NSArray *subArray = self.dataArray[section];
    return subArray.count;
}


- ( UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"id" forIndexPath:indexPath];
    cell.backgroundColor = randomColor;
    return cell;
    
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footer" forIndexPath:indexPath];
    footerView.backgroundColor = [UIColor cyanColor];
    for (UIView *subView in footerView.subviews) {
        [subView removeFromSuperview];
    }
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:footerView.bounds];
    titleLabel.text = [NSString stringWithFormat:@"第%ld个分区的区尾",(long)indexPath.section];
    [footerView addSubview:titleLabel];
    return footerView;
  
}

@end

CB07B05D-0D50-438C-B9AF-5A8C5470E409.png

地址https://github.com/yinbowang/UICollectionVIewDemo.git

你可能感兴趣的:(UICollectionView自定义布局)