UICollectionView

来源:互联网 发布:自由篮球挂机软件 编辑:程序博客网 时间:2024/06/10 05:48

• UICollectionView和UICollectionViewController 类是iOS6新引进的API,用于展示集合视图,布局更加灵活,可实现多列布局,用法类似于UITableView和UITableViewController类;

• 和tableView类似,collectionView使用提供数据的UICollectionViewDataSource和用于处理用户交互的UICollectionViewDelegate;

• UICollectionView使用UICollectionViewLayout处理cell的样式和组织方式,因其实现比较复杂,系统提供了瀑布流UICollectionViewFlowLayout来实现简单的布局。

标准的UICollectionView包含三个部分
• 1.Cells 用于展示内容的主体,对于不同的cell可以指定不同尺寸和不同的内容;
• 2.Supplementary Views 追回视图,可能理解为tableview中每个section的header或者footer;
• 3.Decoration Views 装饰视图,设置每个section的背景
• UICollectionView是一种新的数据展示方式,其最简单的实现方式就是瀑布流布局(UICollectionViewFlowLayout),可以理解为多列的UITableView,例如iBook中图形化展示书籍的方法。
• 与UITableView不同,UICollectionView的布局可以自定义,使得用户可以按照自己喜欢的方式排版界面上的显示,因此采用UICollectionView方式可以让界面布局丰富多彩,灵活多变。
UICollectionView-UICollectionViewDataSource
• - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;//对应分组下单元格数量
• - (UICollectionViewCell )collectionView:(UICollectionView )collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
• - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView;//分组数
• - (UICollectionReusableView )collectionView:(UICollectionView )collectionView viewForSupplementaryElementOfKind:(NSString )kind atIndexPath:(NSIndexPath )indexPath;//section对应的头和尾
• UICollectionView-UICollectionViewDelegate
• - (void)collectionView:(UICollectionView )collectionView didSelectItemAtIndexPath:(NSIndexPath )indexPath;
• - (void)collectionView:(UICollectionView )collectionView didDeselectItemAtIndexPath:(NSIndexPath )indexPath;
• - (void)collectionView:(UICollectionView )collectionView willDisplayCell:(UICollectionViewCell )cell forItemAtIndexPath:(NSIndexPath *)indexPath;
• - (void)collectionView:(UICollectionView )collectionView didEndDisplayingCell:(UICollectionViewCell )cell forItemAtIndexPath:(NSIndexPath *)indexPath;
UICollectionViewFlowLayout常见属性
• scrollDirection:设定scrollView的滚动方向,默认有vertical和horizontal;
• itemSize:定义每一个item的大小。通过设定itemSize可以全局地改变所有cell的尺寸,如果想要对某个cell制定尺寸,可以使用-collectionView:layout:sizeForItemAtIndexPath:方法;
• headerReferenceSize,footerReferenceSize:指定header和footer的大小,根据scrollDirection的设置有关
• minimumIntemSpacing,minimumLineSpacing:分别指定item之间的间隔和每一行之间的间隔,可以理解为item之间的水平间距和垂直间距;
• sectionInset:设置section之间的间隔;