UICollectionView 布局策略

本文详细介绍如何使用UICollectionView创建自定义布局,并实现多种交互效果,包括多选功能、自定义头部和尾部视图、高亮显示及上下文菜单等。



#import "ViewController.h"

#import "CollectionViewCell.h"


static NSString *const CellReuseIdentify =@"CellReuseIdentify";

static NSString *const SupplementaryViewHeaderIdentify =@"SupplementaryViewHeaderIdentify";

static NSString *const SupplementaryViewFooterIdentify =@"SupplementaryViewFooterIdentify";

@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (strong,nonatomic) UICollectionView *collectionView;


@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    [selfsetupViews];

    // Do any additional setup after loading the view, typically from a nib.

}


- (void)setupViews{

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayoutalloc] init];

    layout.minimumInteritemSpacing =10;

    layout.minimumLineSpacing =10;

    layout.itemSize =CGSizeMake((self.view.frame.size.width - 20)/3.0,100);

//    layout.headerReferenceSize = CGSizeMake(100, 10);

//    layout.footerReferenceSize = CGSizeMake(100, 20);

    

    UICollectionView *collectionView = [[UICollectionViewalloc] initWithFrame:self.view.boundscollectionViewLayout:layout];

    collectionView.backgroundColor = [UIColorwhiteColor];

    //是否可以多选,默认为NO,在设置为YES时,会实现多张选择;

    collectionView.allowsMultipleSelection =YES;

    collectionView.dataSource =self;

    collectionView.delegate =self;

    [self.view addSubview:collectionView];

    

    //注册

//    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:CellReuseIdentify];

    [collectionView registerNib:[UINib nibWithNibName:@"CollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:CellReuseIdentify];

    //UICollectionElementKindSectionHeader注册是固定的

    [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:SupplementaryViewHeaderIdentify];

    [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:SupplementaryViewFooterIdentify];

}


#pragma mark - UICollectionViewDataSource method

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

    return10;

}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return9;

}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    CollectionViewCell *cell = (CollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:CellReuseIdentify forIndexPath:indexPath];

    cell.backgroundColor = [UIColor greenColor];

    cell.textLabel.text = [NSString stringWithFormat:@"(%zd,%zd)", indexPath.section, indexPath.row];

    return cell;

}

//头部与尾部

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{

    if([kind isEqualToString:UICollectionElementKindSectionHeader]){

        UICollectionReusableView *supplementaryView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:SupplementaryViewHeaderIdentify forIndexPath:indexPath];

        supplementaryView.backgroundColor = [UIColor cyanColor];

        UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(0,0, 150,20)];

        lable.text = @"2016812";

        [supplementaryView addSubview:lable];

//        supplementaryView.backgroundColor = [UIColor orangeColor];

        return supplementaryView;

    }

    else if([kind isEqualToString:UICollectionElementKindSectionFooter]) {

        UICollectionReusableView *supplementaryView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:SupplementaryViewFooterIdentify forIndexPath:indexPath];

        supplementaryView.backgroundColor = [UIColor redColor];

        return supplementaryView;

    }

    returnnil;

}


#pragma mark - UICollectionViewDelegate method

- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath{

    returnYES;

}

- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath{

    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];

    cell.contentView.backgroundColor = [UIColor blueColor];

}


- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath{

    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];

    cell.contentView.backgroundColor = [UIColor brownColor];

}


- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    returnYES;

}

//第一次点击调用

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    NSLog(@"您刚刚摸了我一下");

}


- (BOOL)collectionView:(UICollectionView *)collectionView shouldDeselectItemAtIndexPath:(NSIndexPath *)indexPath{

    returnYES;

}

//在第一次点击的基础上调用

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{

    NSLog(@"您刚刚轻轻抚摸了我一下");

    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];

    cell.contentView.backgroundColor = [UIColor redColor];

}


- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath{

    returnYES;

}


- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender{

    if ([NSStringFromSelector(action) isEqualToString:@"copy:"]

        || [NSStringFromSelector(action) isEqualToString:@"paste:"])

        returnYES;

    returnNO;

}


- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(nullable id)sender

{

    if([NSStringFromSelector(action) isEqualToString:@"copy:"])

    {

                NSLog(@"-------------执行拷贝-------------");

        [_collectionView performBatchUpdates:^{

            //可变字符串,实现添加删除功能

//            [_section0Array removeObjectAtIndex:indexPath.row];

            [_collectionView deleteItemsAtIndexPaths:@[indexPath]];

        } completion:nil];

    }

    else if([NSStringFromSelector(action) isEqualToString:@"paste:"])

    {

        NSLog(@"-------------执行粘贴-------------");

        [_collectionView performBatchUpdates:^{

            [_collectionView insertItemsAtIndexPaths:@[indexPath]];

        } completion:nil];

    }

}

//

//- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender{

//    NSLog(@"复制之后,可以插入一个新的cell");

//}



#pragma mark - UICollectionViewDelegateFlowLayout method

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{

    if(section ==1){

        return CGSizeMake(100,50);

    }

    return CGSizeMake(100,20);

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值