UICollectionView 集合视图

#import "RootViewController.h"
#import "DetailViewController.h"

static NSString *string = @"cell";
static NSString *headerIndetifier = @"header";
static NSString *footerIndetifier = @"footer";


@interface RootViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor yellowColor];
    self.navigationItem.title = @"集合视图";
    
    //UICollectionView,集合视图, UITableView的好基友,与他十分相似
    //UICollectionView,继承于UIScrollView
    
    //UICollectionViewLayout,集合视图的布局.继承于NSObject,控制和管理集合视图的布局,是抽象基类,不能直接使用
    //UICollectionViewFlowLayout,UICollectionViewLayout的子类,可以使用
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    //cell的大小
    flowLayout.itemSize = CGSizeMake(50, 50);
    //cell在X轴上的间距,默认10
    flowLayout.minimumInteritemSpacing = 10;
    //cell在Y轴上的间距,默认10
    flowLayout.minimumLineSpacing = 10;
    //滚动方向,默认垂直滚动
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;//垂直滚动
    //分区间距
    flowLayout.sectionInset = UIEdgeInsetsMake(50, 20, 50, 20);//上左下右
    //区头大小
    flowLayout.headerReferenceSize = CGSizeMake(375, 50);
    //区尾大小
    flowLayout.footerReferenceSize = CGSizeMake(375, 50);
    
    
    
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
    collectionView.backgroundColor = [UIColor whiteColor];
    collectionView.dataSource = self;//设置数据源
    collectionView.delegate = self;//设置代理
    
    
    //注册cell类型
    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:string];
    //注册区头类型
    [collectionView registerClass:[UICollectionViewCell class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerIndetifier];
    //注册区尾类型
    [collectionView registerClass:[UICollectionViewCell class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:footerIndetifier];


    [self.view addSubview:collectionView];
    [collectionView release];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - UICollectionViewDataSource

//某个分区有多少个cell(单元格)
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 100;
}


//展现cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    //UICollectionViewCell,集合视图单元格,继承于UICollectionReusableView
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:string forIndexPath:indexPath];
    //随机颜色
    cell.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255. green:arc4random() % 256 / 255. blue:arc4random() % 256 / 255. alpha:1.000];
    
    
    
    return cell;
}

//分区个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 2;
}

//创建附加视图(区头和区尾)
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    //根据kind判断是区头或区尾
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        //区头
         UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:headerIndetifier forIndexPath:indexPath];
        headerView.backgroundColor = [UIColor colorWithRed:0.272 green:1.000 blue:0.725 alpha:1.000];
        return headerView;
    }else {
        //区尾
        UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:footerIndetifier forIndexPath:indexPath];
        footerView.backgroundColor = [UIColor colorWithRed:0.738 green:0.970 blue:1.000 alpha:1.000];
        return footerView;
    }
    
}

#pragma mark - UICollectionViewDelegate
//点击了某一个cell
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"点击了:%ld--%ld", indexPath.section, indexPath.item);
    DetailViewController *detailVC = [[DetailViewController alloc] init];
    
    detailVC.color = collectionView.backgroundColor;
    
    [self.navigationController pushViewController:detailVC animated:YES];
    [detailVC release];
}

@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值