UICollectionView 集合视图详解

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值