UICollectionView的出现使得复杂的界面简单化,下面带来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 ViewController
- (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, 25, 10, 25);
//默认最小为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];
//注册
[collectionregisterClass:[UICollectionViewCellclass]forCellWithReuseIdentifier:@"UICollectionViewCell"];
[self.view addSubview:collection];
//头部区域底部区域同cell一样遵循重用机制
[collectionregisterClass:[CollectionReusableViewclass]forSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:@"UICollectionReusableView"];
[collectionregisterClass:[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 = [collectionViewdequeueReusableCellWithReuseIdentifier:@"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 = [collectionViewdequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"UICollectionReusableView"forIndexPath:indexPath];
reusableView.backgroundColor = [UIColor redColor];
return reusableView;
}else{
UICollectionReusableView*reusable = [collectionViewdequeueReusableSupplementaryViewOfKind: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];
}
#import"CollectionReusableView.h"
@interface CollectionReusableView()
@property(nonatomic,retain)UILabel*label;
@end
@implementation CollectionReusableView
- (instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
self.label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
self.label.text = @"Amydom";
self.label.textColor = [UIColor lightGrayColor];
self.label.font = [UIFont systemFontOfSize:20];
[self addSubview:self.label];
}
return self;
}
@end