快速創(chuàng)建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個數(shù)
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
if (self.pictureCategoryModel) {
return self.pictureCategoryModel.showapi_res_body.list.count;
}
else {
return 0;
}
}
//每個section的item個數(shù)
- (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];
// 此循環(huán)是解決組頭視圖復用問題
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);
}