UICollectionView的使用
来源:互联网 发布:windows 评估副本 编辑:程序博客网 时间:2024/06/03 08:34
UICollectionView的使用方法和UITableView很类似,可以参考UITableView的方法
1.UICollectionView的创建(注意这里一定要设置一个布局对象,只有布局对象才能给UICollectionView设置相关属性)
- (void)viewDidLoad{ [super viewDidLoad]; //1.创建布局对象 UICollectionViewFlowLayout *flowLayOut = [[UICollectionViewFlowLayout alloc] init]; //设置单元格的大小 flowLayOut.itemSize = CGSizeMake(90, 90); //设置每一个item之间的最小空隙 flowLayOut.minimumInteritemSpacing = 20; //设置每行之间的最小空隙 flowLayOut.minimumLineSpacing = 20; //设置滑动的方向,默认是垂直滑动// flowLayOut.scrollDirection = UICollectionViewScrollDirectionHorizontal; //设置头视图的大小 flowLayOut.headerReferenceSize = CGSizeMake(375, 40); //2.创建collectionView //UICollectionViewLayout 布局对象 UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:flowLayOut]; //设置代理 collectionView.delegate = self; collectionView.dataSource = self; [self.view addSubview:collectionView]; //注册单元格 [collectionView registerClass:[MyCell class] forCellWithReuseIdentifier:iden]; //注册头视图 [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:header];}
在设置代理的时候跟UITableView设置方法是一样的,但是不同的是签署的协议是不一样的,这是头文件里签署的协议,一定要签布局对象的协议(UICollectionViewDelegateFlowLayout)
@interface ViewController : UIViewController<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
2,代理方法(和UITableView的代理方法类似,首先是两个必须实现的代理方法)
这两个和UITableView一样,是必须实现的//创建单元格的个数- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ if (section == 0) { return 11; }else { return 17; } return 0;}//创建每一个单元格,并设置图片- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ MyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:iden forIndexPath:indexPath]; cell.backgroundColor = [UIColor redColor]; cell.imgName = [NSString stringWithFormat:@"%ld@2x.png",indexPath.row+1]; return cell; }
3,其他常用的代理方法
(1),UICollectionView创建后默认是靠边的,使用此方法可以设置单元格的停靠位置不靠边
//设置每一组视图的停靠位置(可以设置使每一个item不靠边)- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{ // CGFloat top, left, bottom, right; UIEdgeInsets edge = UIEdgeInsetsMake(20, 20, 20, 20); return edge; }使用上面代理方法后单元格的布局效果如图所示:
(2)点击单元格调用的代理方法
//点击单元格调用的代理方法- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ NSLog(@"indexPath:%@",indexPath);}
(3),滚动到指定的单元格
//滚动到指定的单元格(带动画效果)- (void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UICollectionViewScrollPosition)scrollPosition animated:(BOOL)animated;
(4)创建组的头视图
//创建组的头视图- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{ //取得头视图 UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:header forIndexPath:indexPath]; headerView.backgroundColor = [UIColor redColor]; return headerView; }
添加了头视图后的UICollectionView的动态效果图
0 0
- IOS6- UICollectionView的使用
- ios UICollectionView的使用
- UICollectionView的简单使用
- UICollectionView 的使用
- UICollectionView的使用
- UICollectionView的使用
- ios UICollectionView的使用
- [IOS]UICollectionView的使用
- UICollectionView的简单使用
- 8. UICollectionView的使用
- UICollectionView的基本使用
- UICollectionView的使用
- ios UICollectionView的使用
- iOS UICollectionView 的使用
- UICollectionView的基本使用
- UICollectionView 的使用详解
- UICollectionView的简单使用
- ios UICollectionView 的使用
- 黑马程序员——iOS开发须知
- swift-08-使用键值对儿统计字符在字符串中出现的次数
- 当遇到骗子要你转钱时,怎么把骗子弄哭……
- Unity3d5.1简单配置及粒子效果的删除
- Android两种 旋转Bitmap方法
- UICollectionView的使用
- hdu 1277 全文检索(ac自动机模板)
- JGroups 手册
- DeepLearning tutorial(4)CNN卷积神经网络原理简介+代码详解
- 真分页!从微软官网找过来的笔记
- 线程死锁
- Zend_Http_Client 执行http请求 --(手册)
- C#实现将图片另存到新建的文件夹中
- DeepLearning tutorial(5)CNN卷积神经网络应用于人脸识别(详细流程+代码实现)