CollectionView 详细用法

来源:互联网 发布:java回调机制 编辑:程序博客网 时间:2024/06/07 05:42
#import "ViewController.h"#import "CollectionReusableView.h"#define SCREEN_WIDTH [[UIScreen mainScreen]bounds].size.width#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height@interface ViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.//    flowLayout 控制 布局    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];//    section 内置大小    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);//    最小列间距 默认最小是10, 如果小于10才需要写, 想要大于10的话就不用写这个了 ,它会自己去算的 ,行间距类同    flowLayout.minimumInteritemSpacing = 5;//    最小行间距    flowLayout.minimumLineSpacing = 5;//    item 大小, 如果只有一种size,不用代理方法返回大小//    flowLayout.itemSize = CGSizeMake((SCREEN_WIDTH - 30) / 3.0, 200);//    多种item大小 使用预铺可以提高代码运算效率,提高流畅度//    预铺大小 (如果有一个 50,有一个60, 那么通常设置预估 55, 这样可以节省时间)    flowLayout.estimatedItemSize = CGSizeMake(((SCREEN_WIDTH - 30) / 3.0 + SCREEN_WIDTH) / 2.0, 200);//    头部大小    flowLayout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, 100);    flowLayout.footerReferenceSize = CGSizeMake(SCREEN_WIDTH, 80);    UICollectionView *collectionV = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:flowLayout];    collectionV.dataSource = self;    collectionV.delegate = self;//    允许多选    collectionV.allowsMultipleSelection = YES;    collectionV.backgroundColor = [UIColor whiteColor];    [self.view addSubview:collectionV];    [collectionV registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"UICollectionViewCell"];//    header Footer 也叫 增补视图//    collectionView 叫集合视图//    tableView      叫表视图//   collectionView 的header Footer 同cell 一样 需要注册    [collectionV registerClass:[CollectionReusableView class]  forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CollectionReusableView"];    //   collectionView 的header Footer 同cell 一样 需要注册    [collectionV registerClass:[UICollectionReusableView class]  forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"UICollectionElementKindSectionFooter"];}//区数- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {    return 5;}- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{    return 5;}- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"UICollectionViewCell" forIndexPath:indexPath];//    isSelected 可以记录是否被点击    if (cell.isSelected) {        cell.backgroundColor = [UIColor greenColor];    } else {        cell.backgroundColor = [UIColor orangeColor];    }    return cell;}// 增补视图- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {//    head 视图    if (kind == UICollectionElementKindSectionHeader) {        CollectionReusableView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"CollectionReusableView" forIndexPath:indexPath];        headView.backgroundColor = [UIColor grayColor];        return headView;    }//    foot视图    if (kind == UICollectionElementKindSectionFooter) {        UICollectionReusableView *footView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"UICollectionElementKindSectionFooter" forIndexPath:indexPath];        footView.backgroundColor = [UIColor yellowColor];        return footView;    }    return nil;}//每个item 的大小 可以在这个方法里单独设置- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {    if (indexPath.section == 0) {         //    item 大小        return CGSizeMake((SCREEN_WIDTH - 30) / 3.0, 200);    } else {        return CGSizeMake(SCREEN_WIDTH - 20, 200);    }}//点击触发- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {//    获取某个cell    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];    cell.backgroundColor = [UIColor greenColor];}// 取消点击- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {    //    获取某个cell    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];    cell.backgroundColor = [UIColor orangeColor];}
0 0