初始化:
//初始化佈局類(UICollectionViewLayout的子類)
UICollectionViewFlowLayout *fl = [[UICollectionViewFlowLayout alloc]init];
//初始化collectionView
self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:fl];
//設置代理
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
--------------------
須要實現的協議:
UICollectionViewDataSource, UICollectionViewDelegateFlowLayout
PS:UICollectionViewDelegateFlowLayout是UICollectionViewDelegate的子協議
--------------------
註冊相應的UICollectionViewCell子類到collectionView用來從隊列提取和顯示
- (void)registerClass:(Class)cellClass forCellWithReuseIdentifier:(NSString *)identifier;
PS:若是是用nib建立的話,使用下面這個函數來註冊。
- (void)registerNib:(UINib *)nib forCellWithReuseIdentifier:(NSString *)identifier;
若是須要顯示每一個section的headerView或footerView,則還需註冊相應的UICollectionReusableView的子類到collectionView
elementKind是header或footer的標識符,只有兩種能夠設置UICollectionElementKindSectionHeader和UICollectionElementKindSectionFooter
- (void)registerClass:(Class)viewClass forSupplementaryViewOfKind:(NSString *)elementKind withReuseIdentifier:(NSString *)identifier;
PS:若是是用nib建立的話,使用下面這個函數來註冊。
- (void)registerNib:(UINib *)nib forSupplementaryViewOfKind:(NSString *)kind withReuseIdentifier:(NSString *)identifier;
--------------------
實現協議的函數:
跟UITableView的DataSource和Delegate很像,大可自行代入理解。
DataSource:
//每一組有多少個cell
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;
//定義並返回每一個cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
//collectionView裏有多少個組
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView;
//定義並返回每一個headerView或footerView
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath;
上面這個方法使用時必需要注意的一點是,若是佈局沒有爲headerView或footerView設置size的話(默認size爲CGSizeZero),則該方法不會被調用。因此若是須要顯示header或footer,須要手動設置size。
能夠經過設置UICollectionViewFlowLayout的headerReferenceSize和footerReferenceSize屬性來全局控制size。或者經過重載如下代理方法來分別設置
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section;
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section;
Delegate:
//每個cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;
//設置每組的cell的邊界, 具體看下圖
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;ide
//cell的最小行間距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section;
//cell的最小列間距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section;
//cell被選擇時被調用
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath;
//cell反選時被調用(多選時才生效)
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath;函數
例子:佈局
- (void)viewDidLoad {spa
[super viewDidLoad];代理
self.titleLab.text = @"會員信息";orm
UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];blog
layout.headerReferenceSize = CGSizeMake(self.view.bounds.size.width, 50.0f);排序
_collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];隊列
[_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];ci
[self.view addSubview:_collectionView];
[_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.navView.mas_bottom).offset(0);
make.width.equalTo(self.view.mas_width);
make.left.offset(0);
make.bottom.offset(0);
}];
_collectionView.dataSource = self;
_collectionView.delegate = self;
[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"1"];
}
#pragma mark UICollectionViewDataSource,UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return 10;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 3;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"1" forIndexPath:indexPath];
cell.backgroundColor = [UIColor redColor];
return cell;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section;
{
return 20;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
return 20;
}
//每個cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
if(indexPath.section == 0){
return CGSizeMake(50, 50);
}else if(indexPath.section == 1){
return CGSizeMake(self.view.bounds.size.width, 100);
}else{
return CGSizeMake(80, 80);
}
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
UICollectionReusableView *resableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
UILabel *label = (UILabel *)[resableView viewWithTag:100];
if (!label) {
label = [[UILabel alloc] initWithFrame:CGRectMake(0, 5, collectionView.frame.size.width, 40.0f)];
label.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
label.textColor = [UIColor blackColor];
label.tag = 100;
[resableView addSubview:label];
}
if (indexPath.section == 0) {
label.text = @"切換欄目";
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(self.view.bounds
.size.width - 100.0f, 10.0f, 80, 30.0f);
[button setTitle:@"排序刪除" forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
button.layer.borderColor = [UIColor redColor].CGColor;
button.layer.masksToBounds = YES;
button.layer.cornerRadius = 5;
button.layer.borderWidth = 1;
[resableView addSubview:button];
}else{
label.text = @"點擊添加更多欄目";
}
return resableView;
}