iOS UICollectionView的用法
来源:互联网 发布:sql limit 数据库优化 编辑:程序博客网 时间:2024/06/07 09:44
本节内容主要为纯代码方式使用UICollectionView
1.首先是UICollectionView的创建
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];//这里的layout主要是对collectionView的布局。
layout.footerReferenceSize =CGSizeMake(SCREEN_WIDTH,5);//footerReferenceSize对全局的collectionView的footer尺寸进行设定还有与其对应的headerReferenceSize负责设置全局的header的尺寸
//layout.scrollDirection默认是垂直滚动
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
//注册WLGNewsGoodsCommendCell因为系统没有默认的UICollecitonCell进行布局这里是继承自UICollecitonCell进行的布局
[_collectionView registerClass:[WLGNewsGoodsCommendCell class]
forCellWithReuseIdentifier:@"WLGNewsGoodsCommendCell"];
//注册HeaderView
[_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:@"HeaderView"];
/注册FooterView
[_collectionView registerClass:[UICollectionReusableView class]
forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView"];
_collectionView.delegate =self;
_collectionView.dataSource =self;
2.代理方法 数据代理方法:UICollectionViewDataSource
//必须实现的两种
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
//设置分区的cell数量
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
WLGNewsGoodsCommendCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WLGNewsGoodsCommendCell"
forIndexPath:indexPath];
//设置cell此前要对WLGNewsGoodsCommendCell进行注册见UICollectionView的创建
}
//选择实现
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return2;//此方法不实现,默认返回1个分区。
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind
atIndexPath:(NSIndexPath *)indexPath;
{
UICollectionReusableView *reusableView = nil;
if (kind ==UICollectionElementKindSectionHeader) {
UICollectionReusableView *view = [collectionViewdequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"HeaderView"forIndexPath:indexPath];
reusableView = view;
}
if (kind ==UICollectionElementKindSectionFooter) {
UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView"forIndexPath:indexPath];
reusableView = view;
}
return reusableView;//同样这里也要对 HeaderView和 FooterView进行注册见UICollectionView的创建
}
3.布局代理 UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout*)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
//对每一个cell的尺寸进行设置的方法对应全局的 itemSize属性
}
- (CGFloat)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout*)collectionViewLayoutminimumLineSpacingForSectionAtIndex:(NSInteger)section
{
//对每个分区的行间距进行单独设置对应全局的 minimumLineSpacing属性
}
- (CGFloat)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout*)collectionViewLayoutminimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
//对每个分区的列间距进行单独设置对应全局的 minimumInteritemSpacing属性
}
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout*)collectionViewLayoutreferenceSizeForHeaderInSection:(NSInteger)section
{
//对每个分区头尺寸单独设置对应全局的 headerReferenceSize属性
}
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout*)collectionViewLayoutreferenceSizeForFooterInSection:(NSInteger)section
{
//对每个分区的尾尺寸单独设置对应全局的 footerReferenceSize属性
}
4.操作方法的 代理 UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView
didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//选中具体cell的操作方法
}
- ios UICollectionView 的用法
- iOS UICollectionView的用法
- iOS-UICollectionView用法
- UICollectionView的基本用法
- UICollectionView的基本用法
- UICollectionView的用法
- UICollectionView的用法小结
- UICollectionView的基础用法
- UICollectionView的基础用法
- iOS求生之路(四)UICollectionView的用法
- ios UICollectionView的使用
- ios UICollectionView的使用
- [IOS]UICollectionView的使用
- ios UICollectionView的使用
- iOS UICollectionView 的使用
- ios UICollectionView 的使用
- [Swift 开发] UICollectionView的用法
- IOS UICollectionView瀑布流 CHTCollectionViewWaterfallLayout用法
- iOS 批量打包脚本 转
- 网络蜘蛛--抓取一个网页的邮箱
- 一个微信公众号接入另一个微信公众号的内容
- WPF中的binding(七)- 使用LINQ检索作为Binding的数据源
- Centos 7.2 安装 Mysql 5.7.13
- iOS UICollectionView的用法
- Android利用ZXing生成带LOGO的二维码图片
- AR/VR learning (2)--unity3D在android 上的手势识别与检测
- Python如何多次遍历文件及查找特定文件
- Java并发编程-如何终止线程
- NYOJ 2 括号配对问题 (栈 stack)
- 解决火狐浏览器无法用触控屏缩放网页的问题
- 【FastDFS分布式文件系统之三】进程监控及僵死恢复
- Glide学习之二