1.//创建组头组尾一个方法

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

UICollectionReusableView * reusableView = nil;

if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {

reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];

}

}else if ([kind isEqualToString:UICollectionElementKindSectionFooter]) {

reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer" forIndexPath:indexPath];

}

return reusableView;

}

//注册cell   nib

[_collectionView registerNib:[UINib nibWithNibName:@"CollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"CellIdentifier"];

与下面一起使用

- (void)awakeFromNib {

}

class

[self registerClass:UICollectionViewCell.class forCellWithReuseIdentifier:@"item"];

//注册头尾

[self registerClass:UICollectionReusableView.class forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];

[self registerClass:UICollectionReusableView.class forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];

// 指定Section个数

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

return num;

}

// 指定section中的item的个数

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

return num;

}

//下面是代理方法

// 允许选中时,高亮

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

return YES;

}

// 高亮完成后回调

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

}

// 由高亮转成非高亮完成时的回调

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

}

// 设置是否允许选中

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

return YES;

}

// 设置是否允许取消选中

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

return YES;

}

// 选中操作

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

}

// 取消选中操作

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

}

//FlowLayout的相关设置

// 设置每个item上下左右相距

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {

return UIEdgeInsetsMake(5, 5, 5, 5);

}

// 设置最小行间距

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {

return 10;

}

// 设置最小列间距

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {

return 10;

}

// 设置section头视图的参考大小

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

return CGSizeMake(self.view.frame.size.width, 40);

}

// 设置section尾视图的参考大小

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

return CGSizeMake(self.view.frame.size.width, 40);

}

//item的大小设置

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

return CGSizeMake(self.view.frame.size.width / 3 - 10, self.view.frame.size.width / 3 - 10);

}

//cell的重用机制

1: 首先创建一个类,集成 UICollectionViewCell,将你这个UIImageView加到这个类的init方法里面去

2: 在viewDidLoad的时候,注册重用     [self.collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"CELL"];

3: 使用 MyCommentsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CELL" forIndexPath:indexPath];

重用的cell容易引起cell的重叠

原因:在使用UICollectionView的时候会出现在cell上添加视图后重用时视图重叠了,如果只是添加了一张图片在上面可能你不能发现,但添加了UILabel时你就能看到如下的效果,因为在初始化cell时不像UITableView一样调用init的一些方法,所以CollectionView是在init方法是就已经把视图初始化出来了,所以再添加就会重叠,一下2种方法以供使用。

解决方法:

1.移除contentView上的视图再添加视图

2.新建UICollectionViewCell的类,在类中重写initWithFrame方法初始化想要的视图

使用自定义类创建流水的布局

设置每组的列数

- (NSInteger)collectionView:(UICollectionView *)collectionView layout:(STCollectionViewFlowLayout *)layout numberOfColumnsInSection:(NSInteger)section {

return 1;

}

//特点是宽度相同,但是高度不同,需要不同的item赋值不同的size

//不同的indexPath 不同的size

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

if (_isWaterFlow) {

return [[self.itemSizes objectAtIndex:indexPath.row % 4] CGSizeValue];

}

return CGSizeMake(0, 100);

}

下面是设置相关layout的方法:

//这就涉及到了layout里面的相关方法

//返回collectionView的内容的尺寸

-(CGSize)collectionViewContentSize

//返回rect中的所有的元素的布局属性

-(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect

//返回对应于indexPath的位置的cell的布局属性

-(UICollectionViewLayoutAttributes _)layoutAttributesForSupplementaryViewOfKind:(NSString _)kind atIndexPath:(NSIndexPath *)indexPath

//返回对应于indexPath的位置的追加视图的布局属性,如果没有追加视图可不重载

-(UICollectionViewLayoutAttributes * )layoutAttributesForDecorationViewOfKind:(NSString_)decorationViewKind atIndexPath:(NSIndexPath _)indexPath

小插曲://创建桥接文件只需要创建swift项目即可,即可自动生成,不用创建路径

最新文章

  1. .JavaWeb文件上传和FileUpload组件使用
  2. fastjson 混淆注意事项
  3. 金字塔Lucas-Kanande光流算法实现
  4. Host 'XXX' is not allowed to connect to this MySQL server 解决方案/如何开启MySQL的远程帐号
  5. 必须会的SQL语句(二) 创建表、修改表结构、删除表
  6. 获取Application中的spring容器
  7. DHTMLX 前端框架 建立你的一个应用程序 教程(九)--绑定表单Form到表格Grrid中
  8. Controller返回值类型ActionResult
  9. C++----练习--while求和
  10. Spring context:component-scan代替context:annotation-config
  11. oracle 的 SDO_GEOMETRY
  12. MPlayer 使用手册中文版
  13. 使用vmstat和iostat命令进行Linux性能监控【转】
  14. 32 bit 与 64 bit 程序(2)比较
  15. lua去掉字符串中的UTF-8的BOM三个字节
  16. AI通过了艺术创作图灵测试,你根本分不出来作者是不是人
  17. 英文写作强调技巧:alliteration, assonance, consonance
  18. 【刷题】LOJ 6015 「网络流 24 题」星际转移
  19. [转]PostgreSQL 逻辑结构 和 权限体系 介绍
  20. shell编程入门(一)

热门文章

  1. ubuntu12.04+kafka2.9.2+zookeeper3.4.5的伪分布式集群安装和demo(java api)测试
  2. Android定时器Timer.schedule
  3. python之文件操作
  4. python学习-day15:函数作用域、匿名函数、函数式编程、map、filter、reduce函数、内置函数r
  5. 第四周psp
  6. java语句练习1
  7. 【JavaScript】常用方法
  8. zedboard如何从PL端控制DDR读写(七)
  9. java 调用 r, Can't find dependent libraries
  10. Python自动化 【第三篇】:Python基础-集合、文件操作、字符编码与转码、函数