UICollectionView的使用

来源:互联网 发布:淘宝售后在哪里查看 编辑:程序博客网 时间:2024/05/21 17:50

使用瀑布样式列表,即UICollectionView。使用时自定义collectionCell,自定义header,自定义footer。

UICollectionView是继承scrollview的子类,功能与UITableView相似

区别在于如下(使用注意事项):

1、实例化时,多一个设置滚动方向的属性

2、多一个设置UI视图布局的代理协议,以及相关的代理方法

3、每个section中的每个row可以显示多个item

4、一般使用时,使用自定义的UICollectionViewCell视图、UICollectionReusableView页眉、UICollectionReusableView页脚视图(因为系统自带的没有相关字符及图标等的设置方法)


[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #import "ViewController.h"  
  2. #import "CollectionViewHeader.h"  
  3. #import "CollectionViewFooter.h"  
  4. #import "CollectionViewCell.h"  
  5. #import "CollectionVC.h"  
  6.   
  7. @interface ViewController () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>  
  8.   
  9. @end  
  10.   
  11. @implementation ViewController  
  12.   
  13. - (void)viewDidLoad {  
  14.     [super viewDidLoad];  
  15.     // Do any additional setup after loading the view, typically from a nib.  
  16.       
  17.     self.title = @"瀑布视图";  
  18.       
  19.     [self setUI];  
  20. }  
  21.   
  22. - (void)didReceiveMemoryWarning {  
  23.     [super didReceiveMemoryWarning];  
  24.     // Dispose of any resources that can be recreated.  
  25. }  
  26.   
  27. - (void)loadView  
  28. {  
  29.     [super loadView];  
  30.     self.view.backgroundColor = [UIColor whiteColor];  
  31. }  
  32.   
  33. #pragma mark -视图  
  34.   
  35. - (void)setUI  
  36. {  
  37.    //确定是水平滚动,还是垂直滚动  
  38.     UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];  
  39.     [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];  
  40.       
  41.     UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];  
  42.     collectionView.delegate = self;  
  43.     collectionView.dataSource = self;  
  44.     collectionView.autoresizingMask = UIViewAutoresizingFlexibleHeight;  
  45.     [collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:identifierCollectionViewCell];  
  46.     collectionView.allowsSelection = YES;  
  47.     collectionView.allowsMultipleSelection = NO;  
  48.     collectionView.alwaysBounceVertical = YES;  
  49.     collectionView.backgroundColor = [UIColor whiteColor];  
  50.       
  51.     [collectionView registerClass:[CollectionViewHeader class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:identifierCollectionViewHeader];  
  52.     [collectionView registerClass:[CollectionViewFooter class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:identifierCollectionViewFooter];  
  53.       
  54.     [self.view addSubview:collectionView];  
  55. }  
  56.   
  57. #pragma mark - UICollectionViewDataSource  
  58.   
  59. //定义展示的Section的个数  
  60. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView  
  61. {  
  62.     return 5;  
  63. }  
  64.   
  65. //添加一个补充视图(页眉或页脚)  
  66. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath  
  67. {  
  68.     if ([kind isEqual:UICollectionElementKindSectionHeader])  
  69.     {  
  70.         CollectionViewHeader *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:identifierCollectionViewHeader forIndexPath:indexPath];  
  71.           
  72.         headerView.titleLabel.text = [[NSString alloc] initWithFormat:@"%ld Section", indexPath.section];  
  73.           
  74.         return headerView;  
  75.     }  
  76.     else if ([kind isEqual:UICollectionElementKindSectionFooter])  
  77.     {  
  78.         CollectionViewFooter *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:identifierCollectionViewFooter forIndexPath:indexPath];  
  79.           
  80.         footerView.titleLabel.text = [[NSString alloc] initWithFormat:@"%ld Section", indexPath.section];  
  81.           
  82.         return footerView;  
  83.     }  
  84.       
  85.     return nil;  
  86. }  
  87.   
  88. //定义展示的UICollectionViewCell的个数  
  89. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section  
  90. {  
  91.     if (0 == section)  
  92.     {  
  93.         return 2;  
  94.     }  
  95.     else if (1 == section)  
  96.     {  
  97.         return 5;  
  98.     }  
  99.     else if (2 == section)  
  100.     {  
  101.         return 1;  
  102.     }  
  103.     return 10;  
  104. }  
  105.   
  106. //每个UICollectionView展示的内容  
  107. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath  
  108. {  
  109.     CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifierCollectionViewCell forIndexPath:indexPath];  
  110.       
  111.     NSString *title = [[NSString alloc] initWithFormat:@"%ld个", indexPath.row];  
  112.     cell.titlelabel.text = title;  
  113.       
  114.     return cell;  
  115. }  
  116.   
  117. #pragma mark - UICollectionViewDelegateFlowLayout  
  118.   
  119. //定义每个UICollectionView的大小  
  120. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath  
  121. {  
  122.     return CGSizeMake(widthCollectionViewCell, heightCollectionViewCell);  
  123. }  
  124.   
  125. //定义每个UICollectionView的 margin  
  126. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section  
  127. {  
  128.     return UIEdgeInsetsMake(0.010.010.010.0);  
  129. }  
  130.   
  131. //最小行间距  
  132. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section  
  133. {  
  134.     return 10.0;  
  135. }  
  136.   
  137. //最小列间距  
  138. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section  
  139. {  
  140.     return 0.0;  
  141. }  
  142.   
  143. //设定页眉的尺寸  
  144. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section  
  145. {  
  146.     return CGSizeMake(widthCollectionViewHeader, heightCollectionViewHeader);  
  147. }  
  148.   
  149. //设定页脚的尺寸  
  150. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section  
  151. {  
  152.     return CGSizeMake(widthCollectionViewFooter, heightCollectionViewFooter);  
  153. }  
  154.   
  155. #pragma mark - UICollectionViewDelegate  
  156.   
  157. // UICollectionView被选中时调用的方法  
  158. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath  
  159. {  
  160.     [collectionView deselectItemAtIndexPath:indexPath animated:YES];  
  161. }  
  162.   
  163. //返回这个UICollectionView是否可以被选择  
  164. - (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath  
  165. {  
  166.     return YES;  
  167. }  



自定义UICollectionViewCell
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. .h文件  
  2. #import <UIKit/UIKit.h>  
  3.   
  4. #define widthScreen ([[UIScreen mainScreen] bounds].size.width)  
  5.   
  6. #define widthCollectionViewCell ((widthScreen - 10.0 * 3) / 2)  
  7. static CGFloat const heightCollectionViewCell = 100.0;  
  8.   
  9. static NSString *const identifierCollectionViewCell = @"CollectionViewCell";  
  10.   
  11. @interface CollectionViewCell : UICollectionViewCell  
  12.   
  13. @property (nonatomicstrongUIImageView *imageview;  
  14. @property (nonatomicstrongUILabel *titlelabel;  
  15. @property (nonatomicstrongUILabel *detaillabel;  
  16.   
  17. @end  
  18.   
  19. .m文件  
  20. #import "CollectionViewCell.h"  
  21.   
  22. static CGFloat const originXY = 10.0;  
  23. static CGFloat const heightImage = 60.0;  
  24. static CGFloat const heightLabel = 20.0;  
  25.   
  26. @interface CollectionViewCell ()  
  27.   
  28. @end  
  29.   
  30. @implementation CollectionViewCell  
  31.   
  32. - (instancetype)initWithFrame:(CGRect)frame  
  33. {  
  34.     self = [super initWithFrame:frame];  
  35.     if (self)  
  36.     {  
  37.         self.layer.borderWidth = 0.5;  
  38.         self.layer.borderColor = [UIColor redColor].CGColor;  
  39.           
  40.         [self setUI];  
  41.     }  
  42.       
  43.     return self;  
  44. }  
  45.   
  46. #pragma mark - 视图  
  47.   
  48. - (void)setUI  
  49. {  
  50.     // 60.0 + 20.0 + 20.0  
  51.     self.imageview.frame = CGRectMake(0.00.0, (widthScreen - originXY * 3) / 2, heightImage);  
  52.     [self.contentView addSubview:self.imageview];  
  53.       
  54.     UIView *currentView = self.imageview;  
  55.       
  56.     self.titlelabel.frame = CGRectMake(currentView.frame.origin.x, (currentView.frame.origin.y + currentView.frame.size.height), currentView.frame.size.width, heightLabel);  
  57.     [self.contentView addSubview:self.titlelabel];  
  58.       
  59.     currentView = self.titlelabel;  
  60.       
  61.     self.detaillabel.frame = CGRectMake(currentView.frame.origin.x, (currentView.frame.origin.y + currentView.frame.size.height), currentView.frame.size.width, currentView.frame.size.height);  
  62.     [self.contentView addSubview:self.detaillabel];  
  63. }  
  64.   
  65. #pragma mark - setter  
  66.   
  67. - (UIImageView *)imageview  
  68. {  
  69.     if (!_imageview)  
  70.     {  
  71.         _imageview = [[UIImageView alloc] init];  
  72.         _imageview.backgroundColor = [UIColor orangeColor];  
  73.     }  
  74.       
  75.     return _imageview;  
  76. }  
  77.   
  78. - (UILabel *)titlelabel  
  79. {  
  80.     if (!_titlelabel)  
  81.     {  
  82.         _titlelabel = [[UILabel alloc] init];  
  83.         _titlelabel.backgroundColor = [UIColor greenColor];  
  84.         _titlelabel.textAlignment = NSTextAlignmentCenter;  
  85.     }  
  86.       
  87.     return _titlelabel;  
  88. }  
  89.   
  90. - (UILabel *)detaillabel  
  91. {  
  92.     if (!_detaillabel)  
  93.     {  
  94.         _detaillabel = [[UILabel alloc] init];  
  95.         _detaillabel.backgroundColor = [UIColor brownColor];  
  96.     }  
  97.       
  98.     return _detaillabel;  
  99. }  
  100.   
  101. @end  



自定义表头header(UICollectionReusableView)
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. .h文件  
  2. #import <UIKit/UIKit.h>  
  3.   
  4. #define widthScreen ([[UIScreen mainScreen] bounds].size.width)  
  5.   
  6. #define widthCollectionViewHeader (widthScreen)  
  7. static CGFloat const heightCollectionViewHeader = 30.0;  
  8.   
  9. static NSString *const identifierCollectionViewHeader = @"CollectionViewHeader";  
  10.   
  11. @interface CollectionViewHeader : UICollectionReusableView  
  12.   
  13. /// 标题标签  
  14. @property (nonatomicstrongUILabel *titleLabel;  
  15.   
  16. @end  
  17.   
  18.   
  19. .m文件  
  20. #import "CollectionViewHeader.h"  
  21.   
  22. @interface CollectionViewHeader ()  
  23.   
  24. @end  
  25.   
  26. @implementation CollectionViewHeader  
  27.   
  28. - (id)initWithFrame:(CGRect)frame  
  29. {  
  30.     self = [super initWithFrame:frame];  
  31.     if (self)  
  32.     {  
  33.         self.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3];  
  34.           
  35.         [self setUI];  
  36.     }  
  37.     return self;  
  38. }  
  39.   
  40. #pragma mark - 视图  
  41.   
  42. - (void)setUI  
  43. {  
  44.     self.titleLabel.frame = CGRectMake(10.00.0, (widthCollectionViewHeader - 10.0 * 2), heightCollectionViewHeader);  
  45.     [self addSubview:self.titleLabel];  
  46. }  
  47.   
  48. #pragma mark - setter  
  49.   
  50. - (UILabel *)titleLabel  
  51. {  
  52.     if (!_titleLabel)  
  53.     {  
  54.         _titleLabel = [[UILabel alloc] init];  
  55.         _titleLabel.backgroundColor = [UIColor clearColor];  
  56.         _titleLabel.textColor = [UIColor blackColor];  
  57.         _titleLabel.font = [UIFont systemFontOfSize:16.0];  
  58.     }  
  59.       
  60.     return _titleLabel;  
  61. }  
  62.   
  63. @end  



自定义表尾footer(UICollectionReusableView
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. .h文件  
  2. #import <UIKit/UIKit.h>  
  3.   
  4. #define widthScreen ([[UIScreen mainScreen] bounds].size.width)  
  5.   
  6. #define widthCollectionViewFooter (widthScreen)  
  7. static CGFloat const heightCollectionViewFooter = 20.0;  
  8.   
  9. static NSString *const identifierCollectionViewFooter = @"CollectionViewFooter";  
  10.   
  11. @interface CollectionViewFooter : UICollectionReusableView  
  12.   
  13. /// 标题标签  
  14. @property (nonatomicstrongUILabel *titleLabel;  
  15.   
  16. @end  
  17.   
  18.   
  19. .m文件  
  20. #import "CollectionViewFooter.h"  
  21.   
  22. @implementation CollectionViewFooter  
  23.   
  24. - (id)initWithFrame:(CGRect)frame  
  25. {  
  26.     self = [super initWithFrame:frame];  
  27.     if (self)  
  28.     {  
  29.         self.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.1];  
  30.           
  31.         [self setUI];  
  32.     }  
  33.     return self;  
  34. }  
  35.   
  36. #pragma mark - 视图  
  37.   
  38. - (void)setUI  
  39. {  
  40.     self.titleLabel.frame = CGRectMake(10.00.0, (widthCollectionViewFooter - 10.0 * 2), heightCollectionViewFooter);  
  41.     [self addSubview:self.titleLabel];  
  42. }  
  43.   
  44. #pragma mark - setter  
  45.   
  46. - (UILabel *)titleLabel  
  47. {  
  48.     if (!_titleLabel)  
  49.     {  
  50.         _titleLabel = [[UILabel alloc] init];  
  51.         _titleLabel.backgroundColor = [UIColor clearColor];  
  52.         _titleLabel.textColor = [UIColor redColor];  
  53.         _titleLabel.font = [UIFont systemFontOfSize:10.0];  
  54.     }  
  55.       
  56.     return _titleLabel;  
  57. }  
  58.   
  59. @end  
0 0
原创粉丝点击