UICollectionView详解

来源:互联网 发布:数据库审计产品资料 编辑:程序博客网 时间:2024/06/10 04:49

今天,将和大家一起学习UICollectionView,UIcollectionView自出来后,一直受追捧,确实好用。今天有朋友问我如何添加heardView,我简单的回答:tableview如何添加,那么CollectionView就怎么添加,后来经过自己实验发现确实不是那回事,所以列出一些自己犯的错误,供大家参考。

1.首先实例化一个 UICollectionViewFlowLayout因为要设置item,itemSize,等一些属性。

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];  
  2. layout.itemSize = CGSizeMake(100,50); //CGSizeMake(200 , 200);  
  3. layout.sectionInset = UIEdgeInsetsMake(5555);  
  4. layout.minimumLineSpacing = 5;  



2.实例化一个UICollectionView

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. myCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(020self.view.frame.size.heightself.view.frame.size.width) collectionViewLayout:layout];  
  2.       
  3.     myCollectionView.delegate = self;  
  4.     myCollectionView.dataSource = self;  
  5.       
  6.     myCollectionView.backgroundColor = [UIColor blueColor];  
  7.     [myCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"CollectionViewIdentifier"];  
  8.      [myCollectionView registerClass:[CollectionHeadView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head"];  
  9.     [self.view addSubview:myCollectionView];  



3.设置数据源和代理方法

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #pragma mask - dataSource  
  2.   
  3. -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{  
  4.       
  5.     UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewIdentifier" forIndexPath:indexPath];  
  6.     for (UIView*view in cell.contentView.subviews) {  
  7.         if (view) {  
  8.             [view removeFromSuperview];  
  9.         }  
  10.     }  
  11.     UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(554040)];  
  12.     lab.backgroundColor = [UIColor yellowColor];  
  13.     lab.text = [NSString stringWithFormat:@"%d",[indexPath row]+[indexPath section]*3];  
  14.     cell.backgroundColor = [UIColor redColor];  
  15. //    cell.backgroundView.backgroundColor = [UIColor whiteColor];  
  16.     [cell.contentView addSubview:lab];  
  17.     UIView *view = [[UIView alloc] initWithFrame:cell.bounds];  
  18.     [view setBackgroundColor:[UIColor whiteColor]];  
  19.     cell.selectedBackgroundView = view;  
  20.   
  21.   
  22.     return cell;  
  23.       
  24. };  
  25.   
  26. //cell的大小  
  27. -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{  
  28.     return CGSizeMake(5050);  
  29. }  
  30.   
  31.   
  32.   
  33. -(NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{  
  34.     return 10;  
  35. };  
  36. -(NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView{  
  37.     return 10;  
  38. };  
  39.   
  40. #pragma mask - delegate  
  41.   
  42. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{  
  43.     NSLog(@"点击%@",indexPath);  
  44.       
  45. }  
  46.   
  47.   
  48. - (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath  
  49. {  
  50.       
  51.     return YES;  
  52. }  


4.设置heardView,hearView需要自定义,并且继承


5.自定义完成后,需要在代理方法中从缓存池中找到已经注册的heardView

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.   
  4.     CollectionHeadView *heard = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head" forIndexPath:indexPath];  
  5.       
  6.     heard.lable.text = [NSString stringWithFormat:@"第%d组",indexPath.section];  
  7.     return heard;  
  8. }  
  9. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {  
  10.     return CGSizeMake(32040);  
  11. }  

6.效果图

大家按照代码一步一步来,肯定就是超简单。



0 0
原创粉丝点击