UICollectionView的使用介绍
来源:互联网 发布:淘宝店铺点击率 编辑:程序博客网 时间:2024/06/06 05:33
UICollectionView的使用介绍
使用的时候,需要包含
UICollectionViewDataSource,
UICollectionViewDelegate,
UICollectionViewDelegateFlowLayout这三个;
1、UICollectionView的初始化,这里cell及header采用的是xib文件
UICollectionViewFlowLayout *flowLayout=[[[UICollectionViewFlowLayout alloc] init] autorelease]; [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical]; self.collectionView=[[[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout] autorelease]; self.collectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; self.collectionView.dataSource=self; self.collectionView.delegate=self; [self.collectionView setBackgroundColor:[UIColor clearColor]]; self.collectionView.backgroundColor = [UIColor blackColor]; //注册Cell,必须要有 [self.collectionView registerNib:[UINib nibWithNibName:@"FileCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"FileCell"]; //注册HeaderView,根据需求 [self.collectionView registerNib:[UINib nibWithNibName:@"FileCollectionReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"FileCollectionReusable"]; [self.view addSubview:self.collectionView];
2、UICollectionView DataSource
#pragma mark -- UICollectionViewDataSource//定义展示的UICollectionViewCell的个数-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ return 10;}//定义展示的Section的个数-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{ return 2;}- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{ FileCollectionReusableView *reusableview = nil; if (kind == UICollectionElementKindSectionHeader) { reusableview = (FileCollectionReusableView*)[collectionView dequeueReusableSupplementaryViewOfKind :kind withReuseIdentifier:@"FileCollectionReusable" forIndexPath:indexPath]; // header view 处理代码 } return reusableview;}//每个UICollectionView展示的内容-(CHFileCollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ static NSString * CellIdentifier = @"FileCell"; FileCollectionViewCell * cell = (FileCollectionViewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath]; // cell 的显示处理 return cell;}
3、UICollectionViewDelegateFlowLayout
//返回头headerView的大小-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{ CGSize size= CGSizeMake(self.view.frame.size.width, 30); return size;}//定义每个UICollectionView 的大小- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{ CGFloat collectionCellWidth = (self.view.frame.size.width - 5 * 6)/3; return CGSizeMake(collectionCellWidth, collectionCellWidth+30);}//定义每个UICollectionView 的 margin-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{ return UIEdgeInsetsMake(5, 5, 5, 5);}
4、UICollectionViewDelegate
//UICollectionView被选中时调用的方法-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{}//返回这个UICollectionView是否可以被选择-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath{ return YES;}
0 0
- UICollectionView的使用介绍
- UICollectionView 使用 介绍
- UICollectionView 使用 介绍
- UICollectionView 使用 介绍
- UICollectionView 使用 介绍
- UICollectionView 使用 介绍
- UICollectionView 使用 介绍
- UICollectionView之介绍使用
- UICollectionView的简单介绍
- UICollectionView的简单介绍
- UICollectionView的简单介绍
- UITableView的兄弟,UICollectionView介绍
- iOS UICollectionView的详细介绍
- IOS6- UICollectionView的使用
- ios UICollectionView的使用
- UICollectionView的简单使用
- UICollectionView 的使用
- UICollectionView的使用
- C#制作、打包、签名、发布Activex全过程
- MyBatis连接MySQL出错:No operations allowed after connection closed
- 171 聪明的kk
- HTTP与HTTPS SSL
- Android开发:进度条对话框
- UICollectionView的使用介绍
- (精)构建高并发高可用的电商平台架构实践
- EHCache 详解
- c# 进程间同步实现 进程之间通讯的几种方法
- Java多线程实践之—同步篇
- Win10没有推送,手动安装补丁和工具升级的方法
- TCP/IP详解 卷I:协议 のping和traceroute
- oracle用触发器实现插入记录序列自增长(也可以指定记录序号)
- iOS开发 -- "AppDelegate.m"文件 程序启动流程