iOS CollectionViewController
添加CollectionViewController 的协议
<UICollectionViewDelegate,UICollectionViewDataSource>
添加控件
@property (nonatomic,strong)UICollectionView *collectionV;
==============创建UICollectionViewControllerFlowLayout 布局对象=============
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
//设置UICollectionView 中 各个单元格的大小
flowLayout.itemSize = CGSizeMake(100 , 200);
//设置该UICollectionView 只支持垂直滚动
// UICollectionViewScrollDirectionVertical, 垂直
// UICollectionViewScrollDirectionHorizontal 水平
flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
//设置各分区上下左右空白的大小
flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
//设置两行单元格之间的行距
flowLayout.minimumLineSpacing = 10;
//设置两行单元格之间的间距
flowLayout.minimumInteritemSpacing = 10;
flowLayout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 30);
flowLayout.footerReferenceSize = CGSizeMake(self.view.frame.size.width, 30);
//设置布局对象
self.collectionV = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:flowLayout];
//设置CollectionView代理
self.collectionV.delegate = self;
self.collectionV.dataSource = self;
//将CollectionView添加到 本View
[self.view addSubview:self.collectionV];
//注册CollectionViewCell
[self.collectionV registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"cyy"];
//注册CollectionView头视图
[self.collectionV registerClass:[HearderCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
//注册CollectionView尾视图
[self.collectionV registerClass:[FooterCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];
[self.collectionV reloadData];
----------------------------- 代理方法 ----------------------------
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
//返回数组的个数 3;
return oneArr.count;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
//将数组的单个分区 解出来
NSArray *arr = oneArr[section];
//然后返回单个分区的个数
return arr.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
//实现CollectionView的 复用
MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cyy" forIndexPath:indexPath];
//给cell 加一个自定义颜色
cell.label.backgroundColor = [self randomColor];
//实现UIImageView.image 的赋值
cell.imageV.image = [UIImage imageNamed:twoArr[indexPath.section][indexPath.row]];
//实现UILabel.text 的赋值
cell.label.text = oneArr[indexPath.section][indexPath.row];
return cell;
}
-(UIColor *)randomColor{
CGFloat r = arc4random() % 300/255.0;
CGFloat g = arc4random() % 300/255.0;
CGFloat b = arc4random() % 300/255.0;
UIColor *color = [UIColor colorWithRed:r green:g blue:b alpha:1];
return color;
}
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
HearderCollectionReusableView *hearView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
hearView.hearderLB.text = hearArr[indexPath.row];
return hearView;
}else{
FooterCollectionReusableView *footView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footer" forIndexPath:indexPath];
footView.footerLB.text = footArr[indexPath.row];
return footView;
}
}