CollectionView

//集合視圖

//創(chuàng)建全局靜態(tài)重用標識符

static NSString * collectionID = @"id";

@interface ViewController ()

//代理

<UICollectionViewDelegate,UICollectionViewDataSource>


@end


@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];


//特點一:創(chuàng)建CollectionView之前,先要創(chuàng)建CollectionView中的布局

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

//指定每個方格的尺寸

layOut.itemSize = CGSizeMake(310, 150);

//指定每個方格的邊距 上、左、下、右

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

//橫向item最小間距

layOut.minimumInteritemSpacing = 0;

//設置縱向item最小間距

layOut.minimumLineSpacing = 10;

//更改CollectionView的滾動方向

//? ? layOut.scrollDirection = UICollectionViewScrollDirectionHorizontal;

//如果collectionView需要頭尾視圖則必須在layOut中指定頭尾視圖對應的尺寸

layOut.headerReferenceSize = CGSizeMake(320, 100);

UICollectionView * myCollectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layOut];

//特點四:在使用CollectionView的時候,切記將CollectionView的背景顏色清空

myCollectionView.backgroundColor = [UIColor clearColor];

//在創(chuàng)建collectionView的時候,同時就要指定當前CollectionView中要使用的cell

[myCollectionView registerClass:[CustomCollectionViewCell class] forCellWithReuseIdentifier:collectionID];

//特點五:CollectionView的頭尾視圖

[myCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];

myCollectionView.delegate = self;

myCollectionView.dataSource = self;

[self.view addSubview:myCollectionView];

}

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

{

return 24;

}

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

{

//特點二:重用

CustomCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:collectionID forIndexPath:indexPath];

//? ? cell.backgroundColor = [UIColor redColor];

//特點三:collectionView的cell只有contentView

for (UIView * v in [cell.contentView subviews]) {

[v removeFromSuperview];

}

UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 30, 100, 30)];

label.text = @"Cell";

[cell.contentView addSubview:label];

return cell;

}

//負責添加collectionView的頭尾視圖

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

{

UICollectionReusableView * RView = nil;

if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {

//頭視圖

RView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];

UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 100)];

view.backgroundColor = [UIColor greenColor];

[RView addSubview:view];

}else{

}

return RView;

}

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

{

NSLog(@"%ld",indexPath.item);

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容