UICollectionView的使用
1.要點(diǎn)說(shuō)明
1.三個(gè)代理前兩個(gè)的用法和tableView的很像放钦,第三個(gè)是布局的協(xié)議最筒。(注意:頭視圖尾視圖都是由代理方法獲得,而且需要寫注冊(cè)辙培,缺少了也不行邢锯。) 注冊(cè)以后丹擎,就不需要再去管理復(fù)用的問題了。這點(diǎn)就很簡(jiǎn)單再愈。這個(gè)如果用好的話护戳,會(huì)非常的簡(jiǎn)單媳荒。
- 2.item(即cell)的布局原理(類似tableView)
- 3.header/footer View的布局原理都是繼承于UICollectionReusableView,并且必須要從dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:這個(gè)代理方法中獲取缴渊,而且根據(jù)不同的kind作為headerView或者footerView衔沼,切記:collectionView中頭部視圖和尾部視圖也需要注冊(cè)
2.代碼
/******************************** UIViewController.h **************************/
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>{
}
@property (strong, nonatomic)UICollectionView *collectionView;
@end
/******************************** UIViewController.m **************************/
#import "UIViewController.h"
#import "UICollectionHeaderView.h"
#import "UICollectionFooterView.h"
#import "UICollectionViewCell.h"
@interface UIViewController ()
@end
//cell俐巴、header、footerView 標(biāo)識(shí)
static NSString *cellIdentifier = @"UICollectionViewCell";
static NSString *headerIdentifier = @"UICollectionHeaderView";
static NSString *footerIdentifier = @"UICollectionFooterView";
@implementation UIViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//創(chuàng)建布局對(duì)象
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
//flowlaout的屬性缘圈,確定是水平滾動(dòng)袜蚕,還是垂直滾動(dòng)
flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
//接下來(lái)就在創(chuàng)建collectionView的時(shí)候初始化,帶上布局方法.
self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 200, 320, 280) collectionViewLayout:flowLayout];
self.collectionView.backgroundColor = [UIColor whiteColor];
//添加View 上面
[self.view addSubview:self.collectionView];
//指定數(shù)據(jù)源和代理
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
**注冊(cè)方法分2種:
1.代碼創(chuàng)建的方式
2.xib 方式創(chuàng)建**
//collectionCell的注冊(cè)
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:cellIdentifier];
//collection頭視圖的注冊(cè)遣疯。
**注意:和TableView不同缠犀,頭視圖和尾部視圖也得注冊(cè)**
[self.collectionView registerClass:[UICollectionHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerIdentifier];
[self.collectionView registerClass:[UICollectionFooterView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:footerIdentifier];
/*另一種方式,從Xib上注冊(cè)
UINib *nib = nil;
nib = [UINib nibWithNibName:cellIdentifier bundle:nil];
[self.collectionView registerNib:nib
forCellWithReuseIdentifier:cellIdentifier];
//注冊(cè)headerview和footerview
nib = [UINib nibWithNibName:headerIdentifier bundle:nil];
[self.collectionView registerNib:nib forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerIdentifier];
nib = [UINib nibWithNibName:footerIdentifier bundle:nil];
[self.collectionView registerNib:nib forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:footerIdentifier];
}
**遵守的協(xié)議:**
**UICollectionViewDataSource/Delegate/DelegateFlowLayout**
#pragma mark UICollectionViewDataSource/Delegate
//1.返回多少組(section),此方法不寫默認(rèn)是1組(跟tableview類似)
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 2;
}
#pragma mark required
//2.每組返回多少個(gè)Item滔迈,這個(gè)Item類似tableview中的cell
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 10;
}
//3.返回cell被辑,這里構(gòu)建Item(即cell)
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
//這里是自定義的cell
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
cell.backgroundColor = [UIColor redColor];
return cell;
}
#pragma mark optional
//4.自定義header/footerView
// The view that is returned must be retrieved from a call to -dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:
//這里返回的view必須要從dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:這個(gè)方法中獲取盼理,而且根據(jù)不同的kind作為headerView或者footerView榜揖,切記:collectionView中頭部視圖和尾部視圖也需要注冊(cè)
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
UICollectionReusableView *reusableView = nil;
if (UICollectionElementKindSectionHeader == kind) {
//這里是自定義的頭視圖
UICollectionHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:headerIdentifier forIndexPath:indexPath];
headerView.backgroundColor = [UIColor whiteColor];
return headerView;
}
if (UICollectionElementKindSectionFooter == kind {
//這里是自定義的尾視圖
UICollectionFooterView *footerView = collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:footerIdentifier forIndexPath:indexPath];
footerView.backgroundColor = [UIColor whiteColor];
return footerView;
}
return reusableView;
}
#pragma mark --UICollectionViewDelegateFlowLayout
//5.布局確定每個(gè)Item 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(100, 100);
}
//6.布局確定每個(gè)section內(nèi)的Item距離section四周的間距 UIEdgeInsets
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(10, 10, 10, 10);
}
//7.返回每個(gè)section內(nèi)上下兩個(gè)Item之間的間距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return 10;
}
//8.返回每個(gè)section內(nèi)左右兩個(gè)Item之間的間距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
return 10;
}
//9.返回headerView的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
CGFloat width = CGRectGetWidth(collectionView.bounds);
CGFloat height = width + 86;
return CGSizeMake(width, height);
}
//10.返回footerView的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
CGFloat width = CGRectGetWidth(collectionView.bounds);
CGFloat height = 144;
return CGSizeMake(width, height);
}
#pragma mark --UICollectionViewDelegate
//11.UICollectionView被選中時(shí)調(diào)用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell * cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
cell.backgroundColor = [UIColor blackColor];
NSLog(@"item======%ld",(long)indexPath.item);
NSLog(@"row=======%ld",(long)indexPath.row);
NSLog(@"section===%ld",(long)indexPath.section);
}
//返回這個(gè)UICollectionView是否可以被選擇
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
希望可以幫助到大家.......