星空网 > 软件开发 > 操作系统

iOS UICollectionView简单使用

首先认识一下UICollectionView

[objc] view plaincopyiOS UICollectionView简单使用images/loading.gif' data-original="https://code.csdn.net/assets/CODE_ico.png" width="12" height="12" />iOS UICollectionView简单使用


  1. NS_CLASS_AVAILABLE_IOS(6_0) @interface UICollectionView : UIScrollView  

 

UICollectionView 和 UICollectionViewController 类是iOS6 新引进的API,用于展示集合视图,布局更加灵活,可实现多列布局,用法类似于UITableView 和 UITableViewController 类。

使用UICollectionView 必须实现UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout这三个协议。

 

下面给出一些常用方法,具体的使用可以参考Demo:点我下载  苹果官方Demo:点我下载

[objc] view plaincopyiOS UICollectionView简单使用iOS UICollectionView简单使用


  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.     self.title = @"UICollectionView学习";  
  5.       
  6.     //通过Nib生成cell,然后注册 Nib的view需要继承 UICollectionViewCell  
  7.     [self.collectionView registerNib:[UINib nibWithNibName:@"SQCollectionCell" bundle:nil] forCellWithReuseIdentifier:kcellIdentifier];  
  8.       
  9.     //注册headerView Nib的view需要继承UICollectionReusableView  
  10.     [self.collectionView registerNib:[UINib nibWithNibName:@"SQSupplementaryView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kheaderIdentifier];  
  11.     //注册footerView Nib的view需要继承UICollectionReusableView  
  12.     [self.collectionView registerNib:[UINib nibWithNibName:@"SQSupplementaryView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:kfooterIdentifier];  
  13.     //  
  14.     self.collectionView.allowsMultipleSelection = YES;//默认为NO,是否可以多选  
  15.       
  16. }  
  17.   
  18. - (void)didReceiveMemoryWarning  
  19. {  
  20.     [super didReceiveMemoryWarning];  
  21.     // Dispose of any resources that can be recreated.  
  22. }  
  23. #pragma mark -CollectionView datasource  
  24. //section  
  25. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView  
  26. {  
  27.     return 2;  
  28. }  
  29. //item个数  
  30. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section  
  31. {  
  32.     return 6;  
  33.       
  34. }  
  35.   
  36. // The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:  
  37. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath  
  38. {  
  39.     //重用cell  
  40.     UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kcellIdentifier forIndexPath:indexPath];  
  41.     //赋值  
  42.     UIImageView *imageView = (UIImageView *)[cell viewWithTag:1];  
  43.     UILabel *label = (UILabel *)[cell viewWithTag:2];  
  44.     NSString *imageName = [NSString stringWithFormat:@"%ld.JPG",(long)indexPath.row];  
  45.     imageView.image = [UIImage imageNamed:imageName];  
  46.     label.text = imageName;  
  47.       
  48.     cell.backgroundColor = [UIColor redColor];  
  49.     return cell;  
  50.       
  51. }  
  52. // The view that is returned must be retrieved from a call to -dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:  
  53. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{  
  54.       
  55.     NSString *reuseIdentifier;  
  56.     if ([kind isEqualToString: UICollectionElementKindSectionFooter ]){  
  57.         reuseIdentifier = kfooterIdentifier;  
  58.     }else{  
  59.         reuseIdentifier = kheaderIdentifier;  
  60.     }  
  61.       
  62.     UICollectionReusableView *view =  [collectionView dequeueReusableSupplementaryViewOfKind :kind   withReuseIdentifier:reuseIdentifier   forIndexPath:indexPath];  
  63.       
  64.     UILabel *label = (UILabel *)[view viewWithTag:1];  
  65.     if ([kind isEqualToString:UICollectionElementKindSectionHeader]){  
  66.         label.text = [NSString stringWithFormat:@"这是header:%d",indexPath.section];  
  67.     }  
  68.     else if ([kind isEqualToString:UICollectionElementKindSectionFooter]){  
  69.         view.backgroundColor = [UIColor lightGrayColor];  
  70.         label.text = [NSString stringWithFormat:@"这是footer:%d",indexPath.section];  
  71.     }  
  72.     return view;  
  73. }  
  74. //定义每个UICollectionViewCell 的大小  
  75. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath  
  76. {  
  77.     return CGSizeMake(60, 80);  
  78. }  
  79. //定义每个Section 的 margin  
  80. -(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section  
  81. {  
  82.     return UIEdgeInsetsMake(15, 15, 5, 15);//分别为上、左、下、右  
  83. }  
  84. //返回头headerView的大小  
  85. -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{  
  86.     CGSize size={320,45};  
  87.     return size;  
  88. }  
  89. //返回头footerView的大小  
  90. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section  
  91. {  
  92.     CGSize size={320,45};  
  93.     return size;  
  94. }  
  95. //每个section中不同的行之间的行间距  
  96. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section  
  97. {  
  98.     return 10;  
  99. }  
  100. //每个item之间的间距  
  101. //- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section  
  102. //{  
  103. //    return 100;  
  104. //}  
  105. //选择了某个cell  
  106. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath  
  107. {  
  108.     UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];  
  109.     [cell setBackgroundColor:[UIColor greenColor]];  
  110. }  
  111. //取消选择了某个cell  
  112. - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath  
  113. {  
  114.     UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];  
  115.     [cell setBackgroundColor:[UIColor redColor]];  
  116. }  


效果图如下:
iOS UICollectionView简单使用
原文链接:http://blog.csdn.net/Apple_app/article/details/38867123



原标题:iOS UICollectionView简单使用

关键词:IOS

IOS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流