• iOS UICollectionView简单使用


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

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

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

    [objc] view plain copy
     在CODE上查看代码片派生到我的代码片
    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. }  
  • 相关阅读:
    hihoCoder 1392 War Chess 【模拟】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛)
    HDU 5889 Barricade 【BFS+最小割 网络流】(2016 ACM/ICPC Asia Regional Qingdao Online)
    Codeforces 715B & 716D Complete The Graph 【最短路】 (Codeforces Round #372 (Div. 2))
    Codeforces 715A & 716C Plus and Square Root【数学规律】 (Codeforces Round #372 (Div. 2))
    Codeforces 716A Crazy Computer 【模拟】 (Codeforces Round #372 (Div. 2))
    Codeforces 716B Complete the Word【模拟】 (Codeforces Round #372 (Div. 2))
    HDU 5875 Function 【倍增】 (2016 ACM/ICPC Asia Regional Dalian Online)
    axios 简介与安装
    serializer 功能
    APIview 整理
  • 原文地址:https://www.cnblogs.com/wanghuaijun/p/5601193.html
Copyright © 2020-2023  润新知