UICollectionView详解和UITableView的区别

来源:互联网 发布:淘宝售后服务怎么写 编辑:程序博客网 时间:2024/05/22 03:02

1. UICollectionView 和 UITableView 的UI区别

UICollectionView默认没有表头,  UITableView: 有表头和表尾;

UICollectionView的区里面是项Item, UITableView:区里面是单元格Cell

UICollectionView布局使用UICollectionViewLayOut的子类(UICollectionViewFlowLayOut 流式布局:流式布局的特点就是会自动根据屏幕的宽度适当的显示列数,如果屏幕款显示的列数可能就多,例如iphone 6s Plus, 如果屏幕相对较窄,显示的列数则较少,例如 iphone 4s)

UICollectionView和UITableView都是分区(段)的

影响UICollectionView显示的列数的因素有: 

①:UICollectionView宽度,宽度越大,显示列数越多; 

②:Item宽度, 每一项的宽度越窄,则显示的列数越多; 

③: 每个Item的列间隙,列间距越窄,显示的越多;


影响UICollectionView 行数的因素:

 UICollectionView 的行数是不能显式指定的,行数是系统自动计算出来的,行数的计算因素有: Item 宽度 和 列间隙  共同决定


UICollectionView 滚动方向与Item的排列方向

滚动方向不同,每个Item的出现顺序也不同;

滚动方向如果是垂直方向,那么Item是横着排列,先排第一行,如果一行排列不完,就继续排第二行;

滚动方向如果是水平方向,那么Item是垂直排列,先排第一列,如果一列排列不完,就继续排序第二列,如果有多个分区,每个分区的第一项肯定再第一行,而不会排在排在上一个分区的尾部,而是另起一列

UICollectionViewScrollDirectionVertical(垂直方向) UICollectionViewScrollDirectionHorizontal (水平方向)

                                                                                     由图可见,滚动方向 垂直和水平 第二个Item出现的位置不同

滚动方向和排列方向 成垂直关系, 如下图解:



2.UICollectionView 知识点


UICollectionViewFlowLayout:Item 的流式布局

[objc] view plain copy
  1. @interface UICollectionViewFlowLayout : UICollectionViewLayout  
  2.   
  3. @property (nonatomic) CGFloat minimumLineSpacing;            // 最小行间距  
  4. @property (nonatomic) CGFloat minimumInteritemSpacing;           // 最小内间距(列间距)  
  5. @property (nonatomic) CGSize itemSize;                   // Item 的宽、高  
  6. @property (nonatomic) CGSize estimatedItemSize NS_AVAILABLE_IOS(8_0);  
  7. @property (nonatomic) UICollectionViewScrollDirection scrollDirection;// 滚动方向:垂直和水平  
  8. @property (nonatomic) CGSize headerReferenceSize;   // 段头的宽高  
  9. @property (nonatomic) CGSize footerReferenceSize;       // 段尾的宽高  
  10.   
  11. @property (nonatomic) UIEdgeInsets sectionInset;        // 段的上下左右的内边距 padding(多个分区一般都会设置该值,如果不设置,多个区会连在一块)  
  12.   
  13. @property (nonatomicBOOL sectionHeadersPinToVisibleBounds NS_AVAILABLE_IOS(9_0);  
  14. @property (nonatomicBOOL sectionFootersPinToVisibleBounds NS_AVAILABLE_IOS(9_0);  
  15.   
  16. @end   

UICollectionView 

[objc] view plain copy
  1. @interface UICollectionView : UIScrollView  
  2.   
  3. - (void)registerClass:(nullable Class)cellClass forCellWithReuseIdentifier:(NSString *)identifier;  
  4.   
  5. @end  

UICollectionViewFlowLayout 的属性设置

流式布局的Item设置:
①:使用UICollectionViewFlowLayout的属性进行设置(此种方法是对所有Item都统一设置)
②:使用UICollectionViewDelegateFlowLayout协议进行设置(此种方法即可以统一设置又可以对某些Item进行特殊设置)

UICollectionViewDelegateFlowLayout 协议不是必须要实现的,可以通过属性来设置,例如
[objc] view plain copy
  1. - (void)viewDidLoad {  
  2.     [super viewDidLoad];  
  3.       
  4.     _dataSource = [NSArray arrayWithObjects:@"0"@"1"@"2"@"3"@"4"@"5"@"6"@"7", nil nil];  
  5.       
  6.     UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];  
  7.     layout.scrollDirection = UICollectionViewScrollDirectionVertical;  
  8.     layout.itemSize = CGSizeMake(9696);  
  9.     layout.minimumInteritemSpacing = 20.0;  
  10.     layout.minimumLineSpacing = 20.0;  
  11.     layout.headerReferenceSize = CGSizeMake(100.050.0);// 指定section的头部高度  
  12.     layout.footerReferenceSize = CGSizeMake(100.050.0);  
  13.     layout.sectionInset = UIEdgeInsetsMake(010.0010.0); // 段的内边距 padding(上下左右)  
  14.     UICollectionView * collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];  
  15.     [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];  
  16.     collectionView.delegate = self;  
  17.     collectionView.dataSource = self;  
  18.       
  19.     [self.view addSubview:collectionView];  
  20. }  

或者在创建UICollectionViewDelegateFlowLayout的时候不设置itemSize,然后在协议中设置
[objc] view plain copy
  1. #pragma mark -  
  2. #pragma mark - UICollectionViewDelegateFlowLayout  
  3. // 定义单元格的宽 高  
  4. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {  
  5.     return CGSizeMake(9696);  
  6. }  
  7.   
  8. // 定义 单元格之间的间距  top bottom right left margin  
  9. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {  
  10.     return UIEdgeInsetsMake(10101010);  
  11. }  

Item的宽高

一般情况下每个Item的宽高是一致的,但也可以不一致(需要实现UICollectionViewDelegateFlowLayout协议来定制)

3. 示例代码

① 基础示例:

[objc] view plain copy
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface ViewController : UIViewController <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>  
  4.   
  5. @property (nonatomicretainNSArray * dataSource;  
  6.   
  7. @end  
[objc] view plain copy
  1. #import "ViewController.h"  
  2.   
  3.   
  4. @interface ViewController ()  
  5.   
  6. @end  
  7.   
  8. @implementation ViewController  
  9.   
  10. - (void)viewDidLoad {  
  11.     [super viewDidLoad];  
  12.     self.title = @"UICollectionView Demo";  
  13.       
  14.     _dataSource = [NSArray arrayWithObjects:@"0"@"1"@"2"@"3"@"4"@"5"@"6"@"7", nil nil];  
  15.       
  16.     UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];  
  17.     layout.scrollDirection = UICollectionViewScrollDirectionVertical;  
  18.       
  19.     UICollectionView * collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];  
  20.     [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];  
  21.     collectionView.delegate = self;  
  22.     collectionView.dataSource = self;  
  23.       
  24.     [self.view addSubview:collectionView];  
  25. }  
  26.   
  27. #pragma mark -  
  28. #pragma mark - UICollectionViewDataSource  
  29. // 共有几段  
  30. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {  
  31.     return 1;  
  32. }  
  33.   
  34. // 每段有多少项  
  35. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {  
  36.     return _dataSource.count;  
  37. }  
  38.   
  39. // 单元格内容  
  40. - (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {  
  41.     static NSString * CellIdentifier = @"Cell";  
  42.     UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];  
  43.     cell.backgroundColor = [UIColor grayColor];  
  44.       
  45.     UIView * contentView = [[UIView alloc] init];  
  46.     UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(20205050)];  
  47.     label.text = _dataSource[indexPath.row];  
  48.     [contentView addSubview:label];  
  49.       
  50.     cell.backgroundView = contentView;  
  51.       
  52.     return cell;  
  53. }  
  54.   
  55. #pragma mark -  
  56. #pragma mark - UICollectionViewDelegateFlowLayout  
  57. // 定义单元格的宽 高  
  58. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {  
  59.     return CGSizeMake(9696);  
  60. }  
  61.   
  62. // 定义 单元格之间的间距  top bottom right left margin  
  63. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {  
  64.     return UIEdgeInsetsMake(10101010);  
  65. }  
  66.   
  67. #pragma mark -  
  68. #pragma mark - UICollectionView Deleage  
  69. // 能否选中单元格  
  70. - (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {  
  71.     return YES;  
  72. }  
  73.   
  74. - (void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {  
  75.     NSLog(@"row:%ld----------section: %ld", indexPath.row,  indexPath.section);  
  76. }  

滚动方向:垂直, 分4区段,效果如下                            滚动方向:水平, 分3区段,效果如下


② 段头

[objc] view plain copy
  1. - (void)viewDidLoad {  
  2.     [super viewDidLoad];  
  3.     _dataSource = [NSArray arrayWithObjects:@"0"@"1"@"2"@"3"@"4"@"5"@"6"@"7", nil nil];  
  4.       
  5.     UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];  
  6.     layout.scrollDirection = UICollectionViewScrollDirectionVertical;  
  7.     layout.sectionInset = UIEdgeInsetsMake(20202020);  
  8.     layout.headerReferenceSize = CGSizeMake(30060);       // 设置段的宽高  
  9.       
  10.     UICollectionView * collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];  
  11.     [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];  
  12.       
  13.     // 注册段头  
  14.     [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ReusableView"];  
  15.       
  16.     collectionView.delegate = self;  
  17.     collectionView.dataSource = self;  
  18.       
  19.     [self.view addSubview:collectionView];  
  20. }  
  21.   
  22.   
  23. // 实现协议方法  
  24. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {  
  25.   
  26.     UICollectionReusableView * headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader  
  27.                                                                                withReuseIdentifier:@"ReusableView"  
  28.                                                                                       forIndexPath:indexPath];  
  29.     UIView * contentView = [[UIView alloc] initWithFrame:CGRectMake(00self.view.frame.size.width50)];  
  30.     contentView.backgroundColor = [UIColor grayColor];  
  31.     UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(2010self.view.frame.size.width30)];  
  32.     label.text = [NSString stringWithFormat:@"Section Header: %ld", indexPath.section];  
  33.     label.textAlignment = NSTextAlignmentCenter;  
  34.     [contentView addSubview:label];  
  35.       
  36.       
  37.     [headerView addSubview:contentView];  
  38.     return headerView;  
  39. }  

效果如图:



③ 自定义Cell

[objc] view plain copy
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface CollectionViewCell : UICollectionViewCell  
  4.   
  5. @property (nonatomicretainUIImageView * imageView;  
  6. @property (nonatomicretainUILabel * textLabel;  
  7.   
  8. @end  
[objc] view plain copy
  1. #import "CollectionViewCell.h"  
  2.   
  3. @implementation CollectionViewCell  
  4.   
  5.   
  6. - (id) initWithFrame:(CGRect)frame {  
  7.     if (self = [super initWithFrame:frame]) {  
  8.         self.backgroundColor = [UIColor grayColor];  
  9.           
  10.         CGFloat itemWidth = self.frame.size.width;  
  11.         CGFloat itemHeight = self.frame.size.height;  
  12.           
  13.         _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(00, itemWidth, itemHeight - 30)];  
  14.         [self addSubview:_imageView];  
  15.           
  16.         _textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, itemHeight - 20, itemWidth, 20)];  
  17.         _textLabel.textAlignment = NSTextAlignmentCenter;  
  18.           
  19.         [self addSubview:_textLabel];  
  20.           
  21.     }  
  22.       
  23.     return self;  
  24. }  
  25.   
  26. @end  
[objc] view plain copy
  1. - (void)viewDidLoad {  
  2.     [super viewDidLoad];  
  3.     self.title = @"UICollectionView Demo";  
  4.       
  5.     UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];  
  6.     layout.scrollDirection = UICollectionViewScrollDirectionVertical;  
  7.     layout.sectionInset = UIEdgeInsetsMake(20202020);  
  8.       
  9.     UICollectionView * collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];  
  10.     [collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];   // 注册类型是自定义类型  
  11.     collectionView.delegate = self;  
  12.     collectionView.dataSource = self;  
  13.       
  14.     [self.view addSubview:collectionView];  
  15. }  
  16.   
  17. // 单元格内容  
  18. - (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {  
  19.     static NSString * CellIdentifier = @"Cell";  
  20.     CollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];  
  21.     cell.backgroundColor = [UIColor grayColor];  
  22.       
  23.     // 给自定义的UI赋值  
  24.     cell.imageView.image = [UIImage imageNamed:@"cat"];  
  25.     cell.textLabel.text = @"闰土";  
  26.       
  27.     return cell;  
  28. }  

其他省略掉的代码和上个示例的完全一样,效果如图:


实例代码下载: http://download.csdn.net/detail/vbirdbest/9441547

使用UICollectionView注意事项:
1. 初始化时必须指定布局
2.必须注册Cell
3. 一般要自定义cell

阅读全文
0 0
原创粉丝点击