iOS UICollectionView小結 + 選中效果
#import "ViewController.h"
#import "CollectionReusableView.h"
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT
@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@end
@implementation
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//flowLayout 控制UICollectionView佈局
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayoutalloc]init];
//section內建大小
flowLayout.sectionInset = UIEdgeInsetsMake
//預設最小為10 如果比10小需要重新賦值
// flowLayout.minimumInteritemSpacing = 5;
//行間距
flowLayout.minimumLineSpacing = 5;
//item大小如果只有一種size 不適用代理方法返回大小
// flowLayout.itemSize = CGSizeMake((SCREEN_WIDTH - 30)/ 3, 200);
//預估算,節省時間
//多種item大小 使用estimatedItemSize提高程式碼運算效率,提高流暢度
//flowLayout.estimatedItemSize = CGSizeMake(((SCREEN_WIDTH - 20) + (SCREEN_WIDTH - 30)/3)/2, 200);
//設定header大小
flowLayout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, 50);
// flowLayout.sectionHeadersPinToVisibleBounds = YES;
// flowLayout.footerReferenceSize = CGSizeMake(SCREEN_WIDTH, 50);
UICollectionView *collection = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:flowLayout];
//設定允許多選
collection.allowsMultipleSelection = YES;
//籤代理
collection.delegate = self;
collection.dataSource = self;
collection.backgroundColor = [UIColor whiteColor];
//註冊
[collection registerClass:[UICollectionViewCellclass] forCellWithReuseIdentifier:@"UICollectionViewCell"];
[self.view addSubview:collection];
//頭部區域底部區域同cell一樣遵循重用機制
[collection registerClass:[CollectionReusableViewclass] forSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:@"UICollectionReusableView"];
[collection registerClass:[UICollectionReusableViewclass] forSupplementaryViewOfKind:UICollectionElementKindSectionFooterwithReuseIdentifier:@"UICollectionReusable"];
}
#pragma mark - collection delegate/dataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 3;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
if (section == 0) {
return 1;
}else if (section == 1){
return 4;
}else{
return 5;
}
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"UICollectionViewCell"forIndexPath:indexPath];
if (cell.isSelected) {
cell.backgroundColor = [UIColor grayColor];
}else{
cell.backgroundColor = [UIColor orangeColor];
}
return cell;
}
#pragma mark -
//collView學名是集合檢視 tableView是表檢視
//collView中的foot header 是曽補檢視
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
if (kind == UICollectionElementKindSectionHeader ) {
CollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"UICollectionReusableView"forIndexPath:indexPath];
reusableView.backgroundColor = [UIColor redColor];
return reusableView;
}else{
UICollectionReusableView *reusable = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"UICollectionReusable"forIndexPath:indexPath];
reusable.backgroundColor = [UIColor blackColor];
return reusable;
}
}
//判斷分割槽,可以直接籤代理或者進入flowlayout裡直接複製方法
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section == 0) {
NSLog(@"1111111");
return CGSizeMake(375, 200);
}else if (indexPath.section == 1){
return CGSizeMake(SCREEN_WIDTH / 2 - 50 , 100);
}else{
return CGSizeMake(SCREEN_WIDTH - 20 , 200);
}
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
//根據idenxPath獲取對應的cell
UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
cell.backgroundColor = [UIColor grayColor];
}
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
cell.backgroundColor = [UIColor orangeColor];
}