UI基础 - UICollectionView 02:定制单元格
■ UICollectionViewCell
1. 准备工作
单元格 CustomCell:是 UICollectionViewCell 的子类
区头/区尾 HeaderView:是 UICollectionReusableView 的子类
图片详情页面 SecondViewController:点击单张图片则进入详情页
2. 代码示例:具体实现
// - CustomCell.h
1 #import <UIKit/UIKit.h> 2 @interface CustomCell : UICollectionViewCell 3 4 @property(nonatomic,strong)UILabel *aLabel; 5 @property(nonatomic,strong)UIImageView *imageView; 6 7 @end
// - CustomCell.m
1 #import "CustomCell.h" 2 @implementation CustomCell 3 4 - (id)initWithFrame:(CGRect)frame{ 5 6 self = [super initWithFrame:frame]; 7 if (self) { 8 9 self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)]; 10 [self addSubview:_imageView]; 11 12 self.aLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 70, self.bounds.size.width, 30)]; 13 _aLabel.font = [UIFont systemFontOfSize:14]; 14 [self addSubview:_aLabel]; 15 } 16 return self; 17 } 18 19 @end
// - HeaderView.h
1 #import <UIKit/UIKit.h> 2 @interface HeaderView : UICollectionReusableView 3 4 @property(nonatomic,strong)UILabel *aLabel;// 内含一个 label 5 6 @end
// - HeaderView.m
1 #import "HeaderView.h" 2 @implementation HeaderView 3 4 - (id)initWithFrame:(CGRect)frame{ 5 6 self = [super initWithFrame:frame]; 7 if (self) { 8 9 self.aLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 5, self.bounds.size.width, 20)]; 10 _aLabel.backgroundColor = [UIColor grayColor]; 11 _aLabel.font = [UIFont systemFontOfSize:14]; 12 [self addSubview:_aLabel]; 13 } 14 return self; 15 } 16 17 @end
// - SecondViewController.h
1 #import <UIKit/UIKit.h> 2 @interface SecondViewController : UIViewController 3 4 @property(nonatomic,strong)UIImage *aImage; // pic 5 @property(nonatomic,strong)UIImageView *imageView;// picView 6 @property(nonatomic,strong)UIScrollView *scrollView; 7 8 9 @end
// - SecondViewController.m
1 #import "SecondViewController.h" 2 @implementation SecondViewController 3 4 - (void)viewDidLoad { 5 [super viewDidLoad]; 6 7 self.navigationItem.title = @"图片详情"; 8 [self.view addSubview:self.scrollView]; 9 // 是否自动适应 ScrollView 的内移量:内容视图是否下移 64 开始显示,默认 YES 10 self.automaticallyAdjustsScrollViewInsets = NO; 11 } 12 13 - (UIImageView *)imageView{ 14 15 if (_imageView == nil) { 16 17 self.imageView = [[UIImageView alloc] initWithImage:self.aImage];// 图片有多大,imageView 的尺寸就有多大 18 } 19 return _imageView; 20 } 21 22 - (UIScrollView *)scrollView{ 23 24 if (_scrollView == nil) { 25 26 self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds]; 27 self.scrollView.contentSize = self.aImage.size; 28 [self.scrollView addSubview:self.imageView]; 29 } 30 return _scrollView; 31 } 32 33 @end
// - ViewController.m
1 #import "ViewController.h" 2 #import "CustomCell.h" 3 #import "HeaderView.h" 4 #import "SecondViewController.h" 5 @interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UIScrollViewDelegate> 6 7 @property(nonatomic,strong)NSMutableArray *dataArray; 8 @property(nonatomic,strong)UIImageView *imageView; 9 10 @end 11 12 @implementation ViewController 13 14 - (void)viewDidLoad { 15 [super viewDidLoad]; 16 self.view.backgroundColor = [UIColor cyanColor]; 17 self.navigationItem.title = @"集合视图"; 18 19 // 隐藏标题 20 UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init]; 21 backItem.title = @" "; 22 self.navigationItem.backBarButtonItem = backItem; 23 24 // 加载数据 25 self.dataArray = [NSMutableArray array]; 26 for (int i =0; i < 45; i ++) { 27 UIImage * aImage = [UIImage imageNamed:[NSString stringWithFormat:@"%d.JPG",i+1]]; 28 [self.dataArray addObject:aImage]; 29 } 30 31 // Layout 32 UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init]; 33 flowLayout.itemSize = CGSizeMake(100, 100);// 尺寸默认是 50*50 34 flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical; 35 flowLayout.minimumLineSpacing = 35;// 行距 36 //flowLayout.minimumInteritemSpacing = 100;// item 间距 37 //flowLayout.sectionInset = UIEdgeInsetsMake(30, 0, 0, 0);// 分区偏移量 38 39 // footerReferenceSize 区尾视图 40 // headerReferenceSize 区头视图 41 flowLayout.headerReferenceSize = CGSizeMake(self.view.bounds.size.width, 40); 42 43 // UICollectionView 实例 44 UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) collectionViewLayout:flowLayout]; 45 46 // 调整 collectionView 显示内容的偏移量 47 collectionView.contentInset = UIEdgeInsetsMake(5, 0, 0, 0);// 向下偏移 5 个像素 48 collectionView.dataSource = self; 49 collectionView.delegate = self; 50 51 // 注册区头 52 [collectionView registerClass:[HeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerVw"]; 53 // 注册 cell 54 [collectionView registerClass:[CustomCell class] forCellWithReuseIdentifier:@"cell"]; 55 [self.view addSubview:collectionView]; 56 57 } 58 59 #pragma mark - <UICollectionViewDataSource> 60 // 每分区有几个 Item 61 - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ 62 63 return self.dataArray.count; 64 } 65 66 - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{ 67 return 2; 68 } 69 70 // 创建 cell 71 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 72 73 CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath]; 74 cell.aLabel.textColor = [UIColor redColor]; 75 cell.aLabel.text = [NSString stringWithFormat:@"s:%d,r:%d",indexPath.section,indexPath.row]; 76 UIImage *aImage = self.dataArray[indexPath.row]; 77 cell.imageView.image = aImage; 78 return cell; 79 } 80 81 // 区头视图,同样也可以制定区尾视图(前提是先制定区头大小) 82 -(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{ 83 84 if ([kind isEqualToString:UICollectionElementKindSectionHeader]) { 85 86 HeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerVw" forIndexPath:indexPath]; 87 88 headerView.backgroundColor = [UIColor orangeColor]; 89 headerView.aLabel.text = [NSString stringWithFormat:@"第%d个区头",indexPath.section + 1]; 90 return headerView; 91 } 92 return nil; 93 } 94 95 #pragma mark - <UICollectionViewDelegate> 96 -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ 97 98 // 详情页 99 UIImage *aImage = self.dataArray[indexPath.row]; 100 SecondViewController *secondVC = [[SecondViewController alloc] init]; 101 secondVC.aImage = aImage; 102 [self.navigationController pushViewController:secondVC animated:YES]; 103 } 104 @end
运行效果:ViewController | SecondViewController