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的动态效果图