IOSUICollectionView 集合视图

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

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.     // Do any additional setup after loading the view, typically from a nib.  
  5.     //self.view.backgroundColor = [UIColor redColor];  
  6.       
  7.     UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];  
  8.     // 设置滚动方向  
  9.     layout.scrollDirection = UICollectionViewScrollDirectionVertical;  
  10.     // 设置layout块的大小,决定cell的大小  
  11.     //layout.itemSize = CGSizeMake(100, 100);  
  12.     //layout.minimumLineSpacing = 100;  
  13.     //layout.minimumInteritemSpacing = 100;  
  14.     //layout.headerReferenceSize = CGSizeMake(320, 40);  
  15.     //layout.footerReferenceSize = CGSizeMake(320, 80);  
  16.     //layout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);  
  17.       
  18.     UICollectionView * collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(020, ScreenWidth, ScreenHeight-20) collectionViewLayout:layout];  
  19.     collectionView.delegate = self;  
  20.     collectionView.dataSource = self;  
  21.     // 注册cell的类型,才能实现cell重用  
  22.     [collectionView registerClass:[HMTMyCustomCollectionCell class] forCellWithReuseIdentifier:@"cell"]; 
  23.     // XIB注册   [collectionView registerClass:[HMTMyCustomCollectionCell class] forCellWithReuseIdentifier:@"CELL"];

  24.       
  25.       
  26.     [self.view addSubview:collectionView];  
  27.   
  28. }  
  29.   
  30. //  设置分区  
  31. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{  
  32.   
  33.     return 2;  
  34.   
  35. }  
  36.   
  37. //  定义每个分区上的元素个数  
  38. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{  
  39.   
  40.     return 10;  
  41.   
  42. }  
  43.   
  44. //  设置元素内容  
  45. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{  
  46.   
  47.     static NSString * cellIdentifier = @"cell";  
  48.     HMTMyCustomCollectionCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];  
  49.       
  50.     cell.backgroundColor = [UIColor cyanColor];  
  51.     cell.showLabel.text = [NSString stringWithFormat:@"%ld,%ld",indexPath.section,indexPath.row];  
  52.       
  53.     return cell;  
  54. }  
  55.   
  56.   
  57. //  设置每个元素大小  
  58. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{  
  59.   
  60. //    if (indexPath.row % 2 == 0) {  
  61. //          
  62. //        return CGSizeMake(100, 100);  
  63. //    } else {  
  64. //          
  65. //        return CGSizeMake(50, 150);  
  66. //    }  
  67.     return CGSizeMake(10050);  
  68.   
  69. }  
  70.   
  71. //  定义每个元素的margin(边缘 上-左-下-右)  
  72. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{  
  73.   
  74.     return UIEdgeInsetsMake(0000);  
  75.   
  76. }  
  77.   
  78. //  定义单元格所在行line之间的距离,前一行和后一行的距离  
  79. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{  
  80.       
  81.     return 20;  
  82. }  
  83.   
  84. //  定义每个单元格相互之间的距离  
  85. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{  
  86.       
  87.     return 0;  
  88.   
  89. }  
  90.   
  91. //  设置页眉(水平滑动的时候设置width,垂直滑动的时候设置height)  
  92. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{  
  93.       
  94.     return CGSizeMake(10100);  
  95.   
  96. }  
  97.   
  98. //  设置页脚(水平滑动的时候设置width,垂直滑动的时候设置height)  
  99. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{  
  100.   
  101.     return CGSizeMake(1010);  
  102.   
  103. }  
  104.   
  105. //  点击元素响应方法  
  106. -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{  
  107.   
  108.   
  109. }  
  110.   
  111.   
  112. /*******************************************自定义Cell***************************************************************/  
  113. - (void)createShowLabel{  
  114.   
  115.     _showLabel = [[UILabel alloc]initWithFrame:CGRectMake(1010self.bounds.size.width-20self.bounds.size.height-20)];  
  116.     _showLabel.backgroundColor = [UIColor redColor];  
  117.     [self.contentView addSubview:_showLabel];  
  118.     [_showLabel release];  
  119.       
  120. }  
  121.   
  122. //  重点:防止cell错乱  
  123. - (void)layoutSubviews{  
  124.   
  125.     [super layoutSubviews];  
  126.     _showLabel.frame = CGRectMake(1010self.bounds.size.width-20self.bounds.size.height-20);  
  127.       
  128. }  

@附加:可能最不好理解的就是那个上-左-下-右了,给出2张图作参考

@补充:(storyboard中无需注册)

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //设置headerView的高度  
  2. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section  
  3.   
  4. //设置sectionHeaderView  
  5. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath  
  6.   
  7.  //注册某种类型的headerView  
  8.     [self.collectionView registerClass:[RecipeCollectionHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];   


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值