CollectionViewOS6之后出现的视图控件,与UITableView类似,最大的特点是可以对cell实现灵活的布局
一UICollectionViewFlowLayout 介绍
介绍:UICollectionViewFlowLayout系统提供的,能够实现cell的网格布局
UICollectionViewFlowLayout的父类是UICollectionViewLayout,UICollectionViewLayout集合视图的布局类,是一个抽象基类
二.使用
UICollectionViewFlowLayout *layot = [[UICollectionViewFlowLayout alloc]init];
UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0,80, self.view.frame.size.width,self.view.frame.size.height-80) collectionViewLayout:layot];
CGFloat itemW = (self.view.frame.size.width-20)/3;
CGFloat itemH = itemW;
collectionView.backgroundColor = [UIColor redColor];
layot.itemSize = CGSizeMake(itemW, itemH);
//调整item的内边距
layot.sectionInset = UIEdgeInsetsMake(5,5, 5, 5);
//滚动方向
layot.scrollDirection = UICollectionViewScrollDirectionVertical;
//设置最小行间距 竖直滚动横向 ,注意如果你设置了该属性为0时cell的行间距还是有间隔的话,有可能是图片的contentModel填充方式不对
layot.minimumLineSpacing =5;
//竖直滚动,垂直方向 该属性根据屏幕宽度可以放几根cell剩余的宽度来配置,当cellw款度设定了每行3个那么他们之间的间距是没有的在设置该属性时就会出现拍版错误
layot.minimumInteritemSpacing =0;
//头部视图的宽高
layot.headerReferenceSize = CGSizeMake(self.view.frame.size.width,300);
//尾部视图的宽高
// layot.footerReferenceSize
collectionView.delegate = self;
collectionView.dataSource = self;
[self.view addSubview:collectionView];
//9.0之后只要设置该属性就可以实现collectionview的头部视图悬停效果
// layot.sectionHeadersPinToVisibleBounds = YES;
三.代理方法
#pragma mark---必须要实现datasoucer
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 10;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return array.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
cell.imageView.image = [UIImage imageNamed:array[indexPath.row]];
cell.imageView.contentMode = UIViewContentModeScaleToFill;
cell.backgroundColor = [UIColor yellowColor];
return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"点击了%ld",indexPath.row);
}