UICollectionView 布局策略

    xiaoxiao2025-10-11  12

    #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

    转载请注明原文地址: https://ju.6miu.com/read-1303044.html
    最新回复(0)