ios的collectionView區頭設定

weixin_33895657發表於2016-08-27

直接上程式碼了:

#import "PhotoWallCell.h"

#import "ViewController.h"

#define kScreenWidth [UIScreen mainScreen].bounds.size.width

#define kScreenHeight [UIScreen mainScreen].bounds.size.height

static NSString *collectionCellIndentider = @"collectionCellIndentider";

@interface ViewController ()

@property(nonatomic,weak)UICollectionView*collectionView;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

[self.view addSubview:self.collectionView];

}

- (UICollectionView *)collectionView{

if (_collectionView) {

return _collectionView;

}

CGFloat collectionViewHeight = kScreenHeight - 190;

CGRect frame = CGRectMake(0, 150, kScreenWidth, collectionViewHeight);

UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];

//設定區頭的大小

layout.headerReferenceSize = CGSizeMake(320, 30);

//設定item大小

layout.itemSize =CGSizeMake(100, 100);

//設定區邊間距

layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);

//設定最小間距

[layout setMinimumInteritemSpacing:10.0f];

[layout setMinimumLineSpacing:10.0f];

UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:frame collectionViewLayout:layout];

collectionView.opaque = NO;

collectionView.backgroundColor = self.view.backgroundColor;

collectionView.dataSource = self;

collectionView.delegate = self;

collectionView.pagingEnabled = YES;

collectionView.showsVerticalScrollIndicator = NO;

collectionView.showsHorizontalScrollIndicator = NO;

[self.view addSubview:collectionView];

_collectionView = collectionView;

/*!

*  @brief  註冊cell

*/

[collectionView registerClass:[PhotoWallCell class] forCellWithReuseIdentifier:collectionCellIndentider];

[collectionView registerClass:[PhotoWallCell class]  forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:collectionCellIndentider];

return collectionView;

}

#pragma mark -

#pragma mark -cell Delegate

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

PhotoWallCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:collectionCellIndentider forIndexPath:indexPath];

cell.backgroundColor =[UIColor yellowColor];

return cell;

}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath

{

NSLog(@"你點選了");

}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

return 6;

}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

return 3;

}

//設定session區頭內容

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{

UICollectionReusableView *reusableview = nil;

if (kind == UICollectionElementKindSectionHeader){

UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:collectionCellIndentider forIndexPath:indexPath];

headerView.backgroundColor = [UIColor redColor];

//把想新增的控制元件放在session區頭重用的cell裡,並且回來賦值,防止重用(重點!!!!!)

UILabel *ttLabel = (UILabel *)[headerView viewWithTag:111];

SelectTypeM*mm =_allDate[indexPath.section];

ttLabel.text =mm.proportyName;

reusableview = headerView;

}

return reusableview;

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

相關文章