iOS UICollectionView小结 + 选中效果

来源:互联网 发布:淘宝网卖情趣用品 编辑:程序博客网 时间:2024/05/24 06:30

  UICollectionView的出现使得复杂的界面简单化,下面带来UICollectionView的一些使用细节,以及多选的效果

    新建工程,在viewController .m 里的代码如下:

#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 = [[UICollectionViewalloc]initWithFrame:self.view.framecollectionViewLayout:flowLayout];

    

    //设置允许多选

    collection.allowsMultipleSelection =YES;

   //签代理

    collection.delegate = self;

    collection.dataSource = self;

    collection.backgroundColor = [UIColorwhiteColor];

   //注册

    [collection registerClass:[UICollectionViewCellclass] forCellWithReuseIdentifier:@"UICollectionViewCell"];

    [self.viewaddSubview: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 = [collectionViewdequeueReusableCellWithReuseIdentifier:@"UICollectionViewCell"forIndexPath:indexPath];

    if (cell.isSelected) {

        cell.backgroundColor = [UIColorgrayColor];

    }else{

        cell.backgroundColor = [UIColororangeColor];

    

    }

    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 = [UIColorredColor];

        

        return reusableView;

        

    }else{

        

        UICollectionReusableView *reusable = [collectionViewdequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"UICollectionReusable"forIndexPath:indexPath];

        

        reusable.backgroundColor = [UIColorblackColor];

        

        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 =  [collectionViewcellForItemAtIndexPath:indexPath];

    cell.backgroundColor = [UIColorgrayColor];

    

    

}



- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{

    UICollectionViewCell *cell =  [collectionViewcellForItemAtIndexPath:indexPath];

    cell.backgroundColor = [UIColororangeColor];


}


之后建一个基于UICollectionReusableView的类来设置他的头部和尾部


#import "CollectionReusableView.h"


@interface CollectionReusableView ()


@property (nonatomic ,retain)UILabel *label;


@end



@implementation CollectionReusableView



- (instancetype)initWithFrame:(CGRect)frame{

    

    if (self = [superinitWithFrame:frame]) {

    

        self.label = [[UILabelalloc]initWithFrame:CGRectMake(0,0, frame.size.width, frame.size.height)];

        self.label.text =@"Amydom";

        self.label.textColor = [UIColorlightGrayColor];

        self.label.font = [UIFontsystemFontOfSize:20];

        [self addSubview:self.label];

        

        

    }

    return self;

    

}






@end




关于 UICollectionView 的 重要的属性和值得注意的都在上面的代码中了,希望大家有时间多去研究研究它,确实非常好用



1 0
原创粉丝点击