iOS UICollectionView简介 —— HERO博客
来源:互联网 发布:房天下 源码 编辑:程序博客网 时间:2024/04/30 13:39
UICollectionView简介:
UICollectionView是iOS6引进的API,继承UIScrollView,可以自定义布局展示集合视图,布局灵活,可以多列布局。
UICollectionView属性:
UICollectionViewDataSource、UICollectionViewDelegate、UICollectionViewDelegateFlowLayout:代理协议
cell:展示的内容,可复用,在viewDidLoad方法中注册
supplementary view:追加视图,可复用,在viewDidLoad方法中注册
decoration view:背景视图
itemSize:全局cell尺寸,单独定义用代理方法
minimumLineSpacing:全局行间距
minimumInteritemSpacing:全局cell间距
scrollDirection:滚动方向
headerReferenceSize:全局页眉尺寸
footerReferenceSize:全局页脚尺寸
sectionInset:全局区内边距
UICollectionView使用:
//创建布局
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
//滚动方向
[flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
//组间距
flowLayout1.sectionInset =UIEdgeInsetsMake( , , , );
//注册cell
- (void)registerClass:(nullable Class)cellClass forCellWithReuseIdentifier:(NSString *)identifier
- (void)registerNib:(nullable UINib *)nib forCellWithReuseIdentifier:(NSString *)identifier
//注册supplementary view
- (void)registerClass:(nullable Class)viewClass forSupplementaryViewOfKind:(NSString *)elementKind withReuseIdentifier:(NSString *)identifier
- (void)registerNib:(nullableUINib *)nib forSupplementaryViewOfKind:(NSString *)kind withReuseIdentifier:(NSString *)identifier
//创建cell
- (__kindofUICollectionViewCell *)dequeueReusableCellWithReuseIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath
//创建supplementary view
- (__kindofUICollectionReusableView *)dequeueReusableSupplementaryViewOfKind:(NSString *)elementKind withReuseIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath
#pragma mark <UICollectionViewDataSource>
//组数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
//cell个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
//cell内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
//collectView大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
//cell的最小行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
//collectView的margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
//cell的最小列间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
//设置顶部的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
//追加视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
//点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
//是否可以被选择
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
UICollectionViewLayout简介:
UICollectionViewLayout为UICollectionView提供布局信息,根据需求,我们通常需要自定义一个继承UICollectionViewLayout的类,重载它的方法,达到我们需要的布局。
UICollectionViewLayout重载方法:
//当边界发生改变时,是否应该刷新布局,返回YES表示刷新。
-(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
//collectionView内容的尺寸
-(CGSize)collectionViewContentSize
//rect中的所有的元素的布局属性(cell、追加视图、装饰视图)
-(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
//布局对应indexPath位置的cell属性
-(UICollectionViewLayoutAttributes )layoutAttributesForItemAtIndexPath:(NSIndexPath )indexPath
//布局对应indexPath位置的追加视图属性
-(UICollectionViewLayoutAttributes )layoutAttributesForSupplementaryViewOfKind:(NSString )kind atIndexPath:(NSIndexPath *)indexPath
//布局对应indexPath位置的装饰视图属性
-(UICollectionViewLayoutAttributes * )layoutAttributesForDecorationViewOfKind:(NSString)decorationViewKind atIndexPath:(NSIndexPath )indexPath
- iOS UICollectionView简介 —— HERO博客
- iOS UICollectionView实用练习 —— HERO博客
- iOS UICollectionView实现图片轮播 —— HERO博客
- iOS UIScrollView简介 —— HERO博客
- iOS UITextField简介 —— HERO博客
- iOS UIDynamic简介 —— HERO博客
- iOS UITableView简介 —— HERO博客
- iOS UISearchBar简介 —— HERO博客
- iOS Touch ID指纹识别技术简介 —— HERO博客
- iOS 添加字体库 —— HERO博客
- iOS MD5加密 —— HERO博客
- iOS 预产期计算器 —— HERO博客
- iOS UISearchBar使用 —— HERO博客
- iOS 自定义日历 —— HERO博客
- iOS 自定义键盘 —— HERO博客
- iOS 图片压缩 —— HERO博客
- iOS CAEmitterLayer简介,基于CAEmitterLayer实现粒子效果 —— HERO博客
- iOS MVC设计模式与MVVM设计模式简介 —— HERO博客
- Hbuilder--让手爽,飞一般的编码
- 数组变MaxTree-java
- 3个月零基础入门数据分析师是一件完全可能的事!
- JavaScript用法详解
- sqlserver进程死锁的解决办法(一)
- iOS UICollectionView简介 —— HERO博客
- win7 局域网 svn服务器搭建
- log4j MDC NDC
- Flashback
- Xcode磁盘空间大清理
- 【Linux环境编程】内存管理:函数栈空间,虚拟内存及其分配
- 基于Python的卷积神经网络和特征提取
- GCC交叉编译指令记录
- C++Builder实现内存映射