一、设置 UICollectionViewFlowLayout
// 初始化flowLayout
self.flowLayout = [UICollectionViewFlowLayout new];
// collectionView的item大小
self.flowLayout.itemSize = CGSizeMake((kSCREEN_WIDTH-1)/2, kCollectionViewItemHeight);
// collectionView的item行间距
self.flowLayout.minimumLineSpacing = 1;
// collectionView的item列间距
self.flowLayout.minimumInteritemSpacing = 1;
// collectionView的sectionHeaderView大小
self.flowLayout.headerReferenceSize = CGSizeMake(kSCREEN_WIDTH, 41);
二、设置collectionView
// 使用flowLayout初始化collectionView
self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, self.sdCycleScrollView.frame.size.height, kSCREEN_WIDTH, 1200) collectionViewLayout:self.flowLayout];
self.collectionView.backgroundColor = [UIColor orangeColor];
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
self.collectionView.scrollEnabled = NO;
// 注册item
[self.collectionView registerClass:[xCollectionViewItem class] forCellWithReuseIdentifier:kCollectionItemID];
// 注册sectionHeaderView
[self.collectionView registerClass:[XSectionHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionSectionHeaderID];
三、collectionView代理方法
1、设置collectionView段数
// collectionView段数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
DebugLog(@"section number %lu", (unsigned long)self.functionNameArr.count);
return self.functionNameArr.count;
}
2、设置collectionView每段的item个数
// collectionView每段的item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
NSArray *itemCountArr = self.functionNameArr[section];
DebugLog(@"item number of section %lu", (unsigned long)itemCountArr.count);
return itemCountArr.count;
}
3、设置collectionView的item
// collectionView的item
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
DebugLog(@"set each item");
// 获取自定义的cell
xCollectionViewItem *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionItemID forIndexPath:indexPath];
// 设置自定义cell的图片
NSArray *imgArr = self.functionImageArr[indexPath.section];
NSString *imgString = (NSString *)imgArr[indexPath.row];
cell.itemImageView.image = [UIImage imageNamed:imgString];
//设置自定义cell标签文字
NSArray *nameArr = self.functionNameArr[indexPath.section];
NSString *nameString = (NSString *)nameArr[indexPath.row];
cell.itemLabel.text = nameString;
return cell;
}
4、设置collectionView的sectionHeaderView
// collectionView的sectionHeaderView
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
DebugLog(@"viewForSupplementaryElementOfKind");
// 获取自定义的sectionHeaderView
XSectionHeaderView *sectionHeadView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionSectionHeaderID forIndexPath:indexPath];
// 设置自定义sectionHeaderView的标签文字
sectionHeadView.sectionHeadLabel.text = (NSString *)self.headTextArr[indexPath.section];
return sectionHeadView;
}
5、点击collectionView的item
// 点击collectionView的item
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
// 获取点击的cell
xCollectionViewItem *cell = (xCollectionViewItem *)[collectionView cellForItemAtIndexPath:indexPath];
DebugLog(@"cell name = %@", cell.itemLabel.text);
// 判断cell标签的文字来跳转到相应的控制器
if ([cell.itemLabel.text isEqualToString:@"实时路况"]) {
// 初始化实时路况控制器
RoadConditionViewController *vc = [RoadConditionViewController new];
// 跳转
[self.navigationController pushViewController:vc animated:YES];
}
}