快速创建collectionView的代码
#pragma mark - 设置collectionView界面
- (void)setupUI {
//1.初始化layout
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
//设置collectionView滚动方向
// [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
//设置headerView的尺寸大小
layout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 44);
//该方法也可以设置itemSize
layout.itemSize =CGSizeMake(SCREEN_WIDTH / 3 - 1, 130);
//2.初始化collectionView
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
[self.view addSubview:collectionView];
collectionView.backgroundColor = [UIColor clearColor];
//3.注册collectionViewCell
//注意,此处的ReuseIdentifier 必须和 cellForItemAtIndexPath 方法中 一致 均为 cellId
[collectionView registerClass:[HDUICollectionViewCell class] forCellWithReuseIdentifier:cellId];
//注册headerView 此处的ReuseIdentifier 必须和 cellForItemAtIndexPath 方法中 一致 均为reusableView
[collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reusableView"];
//4.设置代理
collectionView.delegate = self;
collectionView.dataSource = self;
_collectionView = collectionView;
}
#pragma mark collectionView代理方法
//返回section个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
if (self.pictureCategoryModel) {
return self.pictureCategoryModel.showapi_res_body.list.count;
}
else {
return 0;
}
}
//每个section的item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.pictureCategoryModel.showapi_res_body.list[section].list.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
HDUICollectionViewCell *cell = (HDUICollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
cell.title = self.pictureCategoryModel.showapi_res_body.list[indexPath.section].list[indexPath.row].name;
cell.backgroundColor = [UIColor yellowColor];
return cell;
}
//设置每个item的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(SCREEN_WIDTH / 3 - 1, 130);
}
//// footer的size
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
// return CGSizeMake(10, 10);
//}
//
//// header的size
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
// return CGSizeMake(SCREEN_WIDTH, 44);
//}
//设置每个item的UIEdgeInsets
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(0, 0, 0, 0);
}
//设置每个item水平间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
return 0;
}
//设置每个item垂直间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
return 1;
}
//通过设置SupplementaryViewOfKind 来设置头部或者底部的view,其中 ReuseIdentifier 的值必须和 注册是填写的一致,本例都为 “reusableView”
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reusableView" forIndexPath:indexPath];
// 此循环是解决组头视图复用问题
for (UIView *view in headerView.subviews) {
[view removeFromSuperview];
}
headerView.backgroundColor =[UIColor grayColor];
UILabel *label = [[UILabel alloc] initWithFrame:headerView.bounds];
for (NSInteger i = 0; i<self.pictureCategoryModel.showapi_res_body.list.count; i++) {
if (indexPath.section==i) {
label.text = self.pictureCategoryModel.showapi_res_body.list[indexPath.section].name;
}
}
label.font = [UIFont systemFontOfSize:20];
NSString *dLabelString = label.text;
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:dLabelString];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
//第一行头缩进
[paragraphStyle setFirstLineHeadIndent:15.0];
//行间距
//[paragraphStyle setLineSpacing:5.0];
//段落间距
//[paragraphStyle setParagraphSpacing:10.0];
//头部缩进
//[paragraphStyle setHeadIndent:15.0];
//尾部缩进
//[paragraphStyle setTailIndent:250.0];
//最小行高
//[paragraphStyle setMinimumLineHeight:20.0];
//最大行高
//[paragraphStyle setMaximumLineHeight:20.0];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [dLabelString length])];
[label setAttributedText:attributedString];
[headerView addSubview:label];
return headerView;
}
//点击item方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
HDUICollectionViewCell *cell = (HDUICollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
// NSString *msg = cell.title;
// NSLog(@"%@",msg);
}
网友评论