collectionView

 1 //
 2 /*
 3  UICollectionView 类是iOS6 新引进的API,用于展示集合视图,
 4  布局更加灵活,可实现多列布局,用法类似于UITableView类。
 5  - 更新视图:    [collectionView reloadData];
 6  - 自定义UICollectionViewCell,与自定义tableViewCell基本一致
 7  */
 8 
 9 #import "ViewController.h"
10 
11 @interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
12 
13 @end
14 
15 @implementation ViewController
16 
17 - (void)viewDidLoad {
18     [super viewDidLoad];
19     UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:[[UICollectionViewFlowLayout alloc]init]];//创建collectionView,要指定layout
20     collectionView.backgroundColor = [UIColor grayColor];
21     [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];//注册UICollectionViewCell的重用标识,也就是下面我们要使用的cell
22     [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];//注册头视图(每组)
23     //    [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"remnant"];//注册尾视图
24     collectionView.delegate = self;
25     collectionView.dataSource = self;
26     [self.view addSubview:collectionView];
27 }
28 //几个代理方法
29 - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
30 {
31     return 10;//设置组数
32 }
33 - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
34 {
35     return 8;//设置每组单元数,过多会自动换行
36 }
37 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
38 {
39     //设置cell,注意,此处使用的identifier必须与之前注册的保持一致
40     static NSString *identifier = @"cell";
41     UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
42     cell.backgroundColor = [UIColor blueColor];
43 //    cell.selectedBackgroundView = nil;设置选中视图
44     cell.layer.borderWidth = 0.5;
45     cell.layer.borderColor = [UIColor redColor].CGColor;
46     return cell;
47 }
48 //如果想要使用头视图,则必须实现该方法
49 -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
50     //设置每组头视图的尺寸
51     return CGSizeMake(100, 30);
52 }
53 -(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
54     NSLog(@"%s",__FUNCTION__);
55     //设置头/根视图,注意,此处的withReuseIdentifier要与之前注册时使用的完全一致(注意重用机制导致的bug)
56     UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
57     headerView.backgroundColor = [UIColor orangeColor];
58     [headerView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
59     UILabel *lable = [[UILabel alloc]initWithFrame:headerView.bounds];
60     lable.text = [NSString stringWithFormat:@"%zi组的头视图",indexPath.section];
61     lable.textColor = [UIColor blueColor];
62     [headerView addSubview:lable];
63     return headerView;
64 }
65 - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
66 {
67     //否单元被点击
68     NSLog(@"%zi组,%zi列",indexPath.section,indexPath.item);
69 }
70 -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
71     //设置item(视图元素)的尺寸
72     return CGSizeMake(100, 100);
73 }
74 -(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
75     //设置组的边距(上、左、下、右)
76     return UIEdgeInsetsMake(1, 0, 1, 0);
77 }
78 -(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
79     //两个item的列间距
80     return 0;
81 }
82 
83 -(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
84     //如果一组中有多行item,设置行间距
85     return 0;
86 }
87 //-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
88 //    //设置每组尾视图的尺寸
89 //    return CGSizeMake(100, 20);
90 //}
91 - (void)didReceiveMemoryWarning {
92     [super didReceiveMemoryWarning];
93     // Dispose of any resources that can be recreated.
94 }
95 
96 @end

 

posted @ 2015-12-06 15:01  刘冠  阅读(639)  评论(0编辑  收藏  举报