• iOS 关于 UICollectionView常见使用方法


    Step—1:

    1、声明

    @property(nonatomic,strong) UICollectionView*collectionView;

    2、协议

    3、创建

     //创建流式不见你

     UICollectionViewFlowLayout*layout = [[UICollectionViewFlowLayoutalloc] init];

     //设置单元格尺寸

        layout.itemSize= CGSizeMake(150, 150);

     //设置内边距

        layout.sectionInset= UIEdgeInsetsMake(30,10, 0, 10);

     //设置每一行之间的间距

        layout.minimumLineSpacing= 30;

     //设置单元格之间的间距

        layout.minimumInteritemSpacing= 10;

     self.collectionView= [[UICollectionViewalloc] initWithFrame:self.view.framecollectionViewLayout:layout];

     self.collectionView.backgroundColor= [UIColorwhiteColor];

     //设置可重用单元格的标示与单元格类型

        [self.collectionViewregisterClass:[EventCollectionViewCellclass] forCellWithReuseIdentifier:@"cellIdetifier"];

     self.collectionView.delegate= self;

     self.collectionView.dataSource= self;

        [self.viewaddSubview:self.collectionView];

     4、代理

    //节数

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

     return3;

    }

    //每节的列数

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

     return5;

    }

    //显示数据

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

     EventCollectionViewCell*cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdetifier"forIndexPath:indexPath];

        cell.backgroundColor= [UIColorlightGrayColor];

        cell.eventLabel.backgroundColor= [UIColorgrayColor];

        cell.eventLabel.text= @"Title";

        cell.eventImageView.image= [UIImageimageNamed:@"icon"];

         return cell;

    }

    //选中

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

     NSLog(@"选中 indexPath.row  %ld",indexPath.item);

    }

    //取消选中

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

     NSLog(@"取消选中 indexPath.row  %ld",indexPath.item);

    }

  • 相关阅读:
    为什么Android 开发都在意Framework 底层知识?
    Ant design table实现单选和点击行选中
    SIP系统组成格式
    2022年跨境电商卖家减少废弃购物车的7大技巧
    单载波频域均衡matlab仿真,包括卷积编码维特比译码,矩阵交织,QPSK调制解调,导频插入,MMSE-FDE频域均衡
    MySQL之内存篇
    淘宝官方开放平台API接口获取淘宝商品详情信息(价格、销量、优惠价等)
    许愿,点亮心中的希望之火
    【数据结构与算法】二叉树——堆
    k8s~动态生成pvc和pv
  • 原文地址:https://blog.csdn.net/qq_34491373/article/details/126420338