iOS-UICollectionView基本使用
来源:互联网 发布:东方直播室网络女主播 编辑:程序博客网 时间:2024/06/16 11:53
UICollectionView
UICollectionView基于UIScrollView,系统默认内容自动偏移64各单位 ,所以可以通过设置以下设置取消偏移
self.automaticallyAdjustsScrollViewInsets = NO;
UICollectionView必须设置瀑布流UICollectionViewFlowLayout,如:
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
layout.minimumLineSpacing = 5.0;
layout.minimumInteritemSpacing = 5.0;
layout.sectionInset = UIEdgeInsetsMake(5.0, 5.0, 5.0, 5.0);
UICollectionView必须设置数据源和代理,如:
_mineCollection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64.0, DEVICE_WIDTH, DEVICE_HEIGHT - 64.0) collectionViewLayout:layout];
_mineCollection.backgroundColor = [UIColor lightGrayColor];
_mineCollection.dataSource = self;
_mineCollection.delegate = self;
cell必须通过注册,如:
[_mineCollection registerClass:[MineCollectionViewCell class] forCellWithReuseIdentifier:collectionCell];
[_mineCollection registerClass:[CollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:collectionHeader];
cell必须自定义,系统cell没有任何子控件
UICollectionView必须设置数据源和代理
//分区,组
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
//每一分区的单元个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
//集合视图单元格大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
//头部大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
//集合视图头部或者尾部
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
//单元格复用
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
//被选中的单元格
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
UICollectionViewFlowLayout
初始化layout后自动调动,可以在该方法中初始化一些自定义的变量参数
* Invalidate:刷新,在滚动的时候是否允许刷新布局
-(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
return YES;
}
* 设置UICollectionView的内容大小,道理与UIScrollView的contentSize类似,@return 返回设置的UICollectionView的内容大小
-(CGSize)collectionViewContentSize
* 初始Layout外观 @param rect 所有元素的布局属性 @return 所有元素的布局
-(NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect
* 根据不同的indexPath,给出布局 @param indexPath @return 布局
-(UICollectionViewLayoutAttributes )layoutAttributesForItemAtIndexPath:(NSIndexPath)indexPath
```
demo
【版权归作者所有,iOS开发整理发布】
来源:http://mp.weixin.qq.com/s/q7kvj5oRM2o3MSSWLpGgeQ
转载请联系作者授权
↙点击“阅读原文”,加入
『程序员大咖』
- iOS-UICollectionView基本使用
- iOS 心得二 UICollectionView的基本使用
- iOS控件-UICollectionView基本使用详解
- UICollectionView的基本使用
- UICollectionView的基本使用
- UICollectionView的基本使用
- uicollectionview的基本使用
- UICollectionView的基本使用
- UICollectionView的基本使用
- UICollectionView的基本使用
- UICollectionView的基本使用
- UICollectionView的基本使用
- UICollectionView的基本使用
- IOS-UICollectionView的基本使用以及添加headerView
- iOS控件-UICollectionView基本使用详解(OC)
- iOS控件-UICollectionView基本使用详解(OC)
- ios UICollectionView的使用
- UICollectionView使用 - iOS开发
- 哪些 iOS 应用让你用了很满意并能提高生活质量?
- .
- iOS开发 精简TableView
- iOS 11 vs. Android O: 谁是目前的赢家?
- 外国网友爆料,苹果智能眼镜的研发有了这些最新进展
- iOS-UICollectionView基本使用
- iOS大改版之后,App Store的玩法和生态都彻底变了
- iOS 11 来了!你想知道的都在这里
- 让收益达到真正的50%,存款100以上的赶紧打开,她是如何做到的呢……
- 苹果2017年WWDC大会全程回顾(中文同传和英文原声)
- c#上位机实验
- 厉害了我的老奶奶 82岁日本老人成了WWDC最年长开发者
- 如何用最快速度撸个最简单的markdown编辑器
- iOS开发 单例使用问题