基本
調(diào)用協(xié)議:<UICollectionViewDataSource,UICollectionViewDelegate>
//特點(diǎn)一:創(chuàng)建CollectionView之前灯变,先要?jiǎng)?chuàng)建CollectionView中的布局
UICollectionViewFlowLayout* layOut = [[UICollectionViewFlowLayout alloc]init];
//指定每個(gè)方格的尺寸
layOut.itemSize =CGSizeMake(self.view.bounds.size.width \/ 2 - 20, 150);
//指定每個(gè)方格的邊距上谨垃、左松嘶、下、右
`layOut.sectionInset =UIEdgeInsetsMake(10, 10, 10, 10);`
//橫向item最小間距,默認(rèn)為0
layOut.minimumInteritemSpacing= 0;
//設(shè)置縱向item最小間距
//layOut.minimumLineSpacing = 5;
//更改CollectionView的滾定方向
//layOut.scrollDirection = UICollectionViewScrollDirectionHorizontal;
//如果CollectionView需要頭尾視圖躲因,必須在layOut中指定頭尾視圖對(duì)應(yīng)的尺寸(不指定默認(rèn)0癣防,0)
layOut.headerReferenceSize= CGSizeMake(320, 100);
UICollectionView *myCollectionView = [[UICollectionView alloc]initWithFrame:self.view.boundscollectionViewLayout:layOut];
//特點(diǎn)四:在使用collenctionView的時(shí)候歇式,切記將collenctionView的背景顏色清空,默認(rèn)為透明牙捉,不清空則會(huì)變成黑色
myCollectionView.backgroundColor= [UIColor clearColor];
//在創(chuàng)建collenctionView的同時(shí)就要指定當(dāng)前collenctionView中要使用的cell
[myCollectionView registerClass:[CustomCollectionViewCellclass] forCellWithReuseIdentifier:collenctionID];
//特點(diǎn)五:CollectionView的頭尾視圖
[myCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];//重用標(biāo)識(shí)隨便寫
myCollectionView.delegate= self;
myCollectionView.dataSource= self;
[self.viewaddSubview:myCollectionView];
協(xié)議方法:
-(NSInteger)collectionView:(UICollectionView*)collectionView numberOfItemsInSection:(NSInteger)section {
return 24;
}
-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionViewcellForItemAtIndexPath:(NSIndexPath *)indexPath {
//特點(diǎn)二:重用
CustomCollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:collenctionIDforIndexPath:indexPath];
cell.backgroundColor =[UIColor redColor];
//如果不用CustomCollectionViewCell進(jìn)行創(chuàng)建竹揍,則要用for的方式進(jìn)行移除,方法同UITableView,不然會(huì)黑(重復(fù)創(chuàng)建)
//特點(diǎn)三:collenctionView的cell只有contentView
UILabel * label =[[UILabel alloc]initWithFrame:CGRectMake(0, 30, 100, 30)];
label.text =@"Cell";
[cell.contentViewaddSubview:label];
return cell;
}
//負(fù)責(zé)添加CollectionView的頭尾視圖
-(UICollectionReusableView*)collectionView:(UICollectionView *)collectionViewviewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath*)indexPath {
UICollectionReusableView* RView = nil;
if ([kindisEqualToString:UICollectionElementKindSectionHeader]) {
//定制頭視圖
RView =[collectionViewdequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
UIView * view =[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 100)];
view.backgroundColor =[UIColor greenColor];
[RViewaddSubview:view];
} else {
//尾視圖
}
return RView;
}
-(void)collectionView:(UICollectionView*)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"%ld",indexPath.item);
}
CustomCollectionViewCell中的內(nèi)容:
-(id)initWithFrame:(CGRect)frame {
if (self = [superinitWithFrame:frame]) {
self.backgroundColor =[UIColor orangeColor];
}
return self;
}
常用方法
出現(xiàn)問題
當(dāng)設(shè)置collectionView橫向時(shí)邪铲,item大小一致或不一致時(shí)會(huì)出現(xiàn)問題
代碼:
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
// layout.itemSize = CGSizeMake(66 * LHQScaleX, 62);
layout.sectionInset = UIEdgeInsetsMake(0, 15 * LHQScaleX, 0, 0);
//最小item間間距
layout.minimumInteritemSpacing = 30;
// 設(shè)置最小行間距
layout.minimumLineSpacing = 30;
//設(shè)置方向?yàn)闄M向
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
- layout.itemSize在
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
里根據(jù)類型設(shè)置對(duì)應(yīng)大小芬位,此處不設(shè)置 - 當(dāng)item大小一致時(shí)會(huì)出現(xiàn),因?yàn)樵O(shè)置了方向?yàn)闄M向带到,系統(tǒng)默認(rèn)將相鄰item的間距判斷為既是行間距又是列間距昧碉,此時(shí)如果不同時(shí)設(shè)置最小行間距
minimumLineSpacing
,則其大小會(huì)根據(jù)最小行間距默認(rèn)大小(10)展示被饿。也即如果方向?yàn)闄M向UICollectionViewScrollDirectionHorizontal
時(shí)四康,minimumInteritemSpacing
和minimumLineSpacing
都要設(shè)置一樣的數(shù)值 - 當(dāng)我們手動(dòng)設(shè)置
layout.itemSize
,使其有尺寸不一致的item存在時(shí)無需考慮此問題锹漱,可能系統(tǒng)在此處判斷所有item不在同一列箭养,而只是在同一行 - 該問題為iOS本身bug