iOS之UICollectionView的创建与介绍

UICollectionView 在iOS开发中也是非常常用的控件,例如市面上最流行的淘宝,京东都有用到UICollectionView来写页面,一些好看的瀑布流也是采用UICollectionView来实现,总体来说UICollectionView用起来还是很实用方便的;
UICollectionView创建

 UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    CGFloat itemWidth = (MWidth - 40) / 3;
    
    //设置单元格大小
    layout.itemSize = CGSizeMake(itemWidth, itemWidth / 1.6);
    //最小行间距(默认为10)
    layout.minimumLineSpacing = 10;
    //最小item间距(默认为10)
    layout.minimumInteritemSpacing = 10;
    //设置UICollectionView的滑动方向
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    //设置UICollectionView的间距
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 40, MWidth, MHeight - 80 - 220 - 50 - 40) collectionViewLayout:layout];
    
    //遵循CollectionView的代理方法
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    
    _collectionView.backgroundColor = [UIColor clearColor];
    //注册cell
    [_collectionView registerClass:[DiscoverRechangeCenterCell class] forCellWithReuseIdentifier:@"DiscoverRechangeCenterCell"];
    [self.centerView addSubview:self.collectionView];

UICollectionView的代理方法

#pragma mark CollectionViewDelegate
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 1;
    
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.sourceArr.count;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    DiscoverRechangeCenterCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DiscoverRechangeCenterCell" forIndexPath:indexPath];
    
//实现cell的展示效果
    
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    //实现cell的点击方法
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,911评论 25 709
  • (一) 阿布小姐是个乖宝宝,在父母眼里是好孩子,在老师眼里是好学生。但她却和班里“糟糕”的男生恋爱了,而且是早恋,...
    丢了朵朵阅读 3,788评论 0 1
  • 一张泛黄的老照片 勾起了我太多的回忆 记忆里的童年 是你缠好的蜘蛛网 和永远捕不到的绿蝴蝶 是袖珍版的自行车 和永...
    指间执念阅读 1,211评论 0 3
  • 东引瓯越阅读 6,281评论 0 51
  • 昨晚的图片出现了错误,重发。。。 笔:秀意 纸:宝虹细纹200g 颜料:荷尔拜因 绘墨
    点点Xxi阅读 3,567评论 3 15

友情链接更多精彩内容