美文网首页
UICollecionView的创建和属性使用

UICollecionView的创建和属性使用

作者: Lambo316 | 来源:发表于2016-06-28 09:51 被阅读209次

//UICollectionViewLayout 布局类的父类

//UICollectionViewFlowLayout 系统自带布局类

UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];

//设置每个Item的大小

//collectionView中的列数,不是确定,间隔也不是确定

flowLayout.itemSize = CGSizeMake(80, 80);

//列最低间隔

flowLayout.minimumInteritemSpacing = 20;

//行最低间隔

flowLayout.minimumLineSpacing = 20;

//分区内所有item区域距离边框和分区头的宽度

//上、左、下、右逆时针的顺序

flowLayout.sectionInset = UIEdgeInsetsMake(50, 10, 100, 10);

//设置滚动方向

flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;

//创建UICollectionView

UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:[self getWaterFallLayout]];

//UICollectionView 严格遵守MVC模式

collectionView.dataSource = self;

collectionView.delegate = self;

//注册CollectionViewCell

[collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"reuse”];

//注册分区头

[collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView”];

//注册分区尾巴

[collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerView”];

#pragma mark UICollectionView的协议方法

#pragma mark 设置分区个数

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView

#pragma mark 设置分区头和尾

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

{

//collectionView的分区头和分尾都遵从重用机制

//判断类型为头或为尾

if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {

UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView" forIndexPath:indexPath];

headerView.backgroundColor = [UIColor grayColor];

return headerView;

}

else

{

//通过重用标识和类型获取对应的重用view

UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerView" forIndexPath:indexPath];

footerView.backgroundColor = [UIColor yellowColor];

return footerView;

}

}

#pragma mark 返回分区有多少个Item (默认是有一个分区)

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

{

return self.dataArray.count;

}

#pragma mark 点击方法

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

#pragma mark UICollectionViewLayout 的协议方法

#pragma makr 设置分区头的size

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

{

//横向滚动时,高度无效

//纵向滚动时,宽度无效

return CGSizeMake(20, 100);

}

//item在显示之前会调动这个方法布局

//计算所有Item的布局

-(void)prepareLayout

//返回整个布局的contentSize(可滚动区域)

-(CGSize)collectionViewContentSize

相关文章

  • UICollecionView的创建和属性使用

    //UICollectionViewLayout 布局类的父类 //UICollectionViewFlowLay...

  • Android中Button和动画

    目的 掌握按钮的相关属性及其操作,熟悉xml配制动画和代码创建和图片的 相关技术、及其使用 1、Button的属性...

  • 第32课:类

    预习: class、__init__、 9.1 创建和使用类 9.1.1创建Dog类 2,在Python2.7中创...

  • 在UITableViewCell xib 里 添加UIColle

    一:在UITableViewCell xib 里 添加UICollecionView 在注册collection...

  • Spring 表达式语言

    创建和使用解析器 字面量 集合 访问对象属性和集合单元

  • copy,mutableCopy

    属性没有mutableCopy,创建NSMutableString对象时使用copy,赋值的时候还是copy所以创...

  • 线程的创建和控制

    线程的创建和控制 进程和线程的关系:进程提供资源,线程使用资源完成工作 创建线程函数 线程的控制 更多的API 创...

  • UITabBarController的创建和属性

    //创建UITabbarController -- 标签视图控制器 UITabBarController *tab...

  • 创建js对象

    1、对象 2、创建对象的方法 3、遍历对象属性:for...in... 4、使用字面量创建对象 5、使用构造函数创...

  • Kotlin-类和对象

    类和对象 类的创建和java还是比较类似的,属性和方法的定义,创建不用写new 在kotlin中如果要使用类的继承...

网友评论

      本文标题:UICollecionView的创建和属性使用

      本文链接:https://www.haomeiwen.com/subject/jzhtjttx.html