UICollectionView的用法小结
2015-12-11 17:16
381 查看
UICollectionView的简单用法,具体可参考/article/8033855.html,有相关的三篇博客,个人感觉还不错,以下是个人的简单理解与应用,可以满足最简单的没有什么特殊展现形式的UICollectionView,后期鄙人也会不断的学习完善,以下是代码有Demo完整的展示,并且都附有注释详解,应该会很容易理解的,希望对各位有帮助,有啥不足的地方或者意见、建议,真心希望留言,咱们共同学习,一起进步,谢谢!
// // ViewController.m // UICollertionViewDemo // // Created by Red_Coral on 15/7/30. // Copyright (c) 2015年 Red_Coral. All rights reserved. // #import "ViewController.h" #define COLLECTIONVIEW_HEADERSECTION_IDENTIFIER @"collectionViewHeaderSctionView_indentifier" #define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width @interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate> @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init]; [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical]; flowLayout.headerReferenceSize = CGSizeMake([[UIScreen mainScreen] bounds].size.width,100); UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height-2) collectionViewLayout:flowLayout]; //这侧UICollectionViewCell(此处也可以自定义的cell类),但是后面的identifier和下面引用的必须要一致 [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"UICollectionViewCell_identifier"]; //注册UICollectionView的section的Header或者footer,同样也要注意identifier的一致性 [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:COLLECTIONVIEW_HEADERSECTION_IDENTIFIER]; collectionView.backgroundColor = [UIColor redColor]; collectionView.dataSource = self; collectionView.delegate = self; collectionView.shouldGroupAccessibilityChildren = YES; [self.view addSubview:collectionView]; } //确定UICollectionView每个章节有几个元素cell, - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return 10; }
//确定UICollectionView有几个章节,(这个方法可以省略)默认是一个 - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 2; } //制定每个cell将要在试图上展线成的样式及其内容 - (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { static NSString *tempStr = @"UICollectionViewCell_identifier"; UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:tempStr forIndexPath:indexPath]; [cell sizeToFit]; if (!cell) { NSLog(@"无法创建CollectionViewCell时打印,自定义的cell就不可能进来了。"); } cell.backgroundColor = [UIColor greenColor]; return cell; } /* *1、此处为Supplementary Views 追加视图 (类似Header或者Footer) *2、用到的identifier需要和注册的一致(可在此处添加自定义) */ - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { NSLog(@"kind: %@",kind); UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind: UICollectionElementKindSectionHeader withReuseIdentifier:COLLECTIONVIEW_HEADERSECTION_IDENTIFIER forIndexPath:indexPath]; headerView.backgroundColor = [UIColor yellowColor]; return headerView; } //确定每个cell的宽和高 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { //cell的宽、高 return CGSizeMake((SCREEN_WIDTH-3)/4., (SCREEN_WIDTH-3)/4.); } //定义每个UICollectionView的cell的layout情况,ps:你可以将下面的值意义改变,看看效果 -(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { return UIEdgeInsetsMake(10, 0, 0, 0); } //定义每个UICollectionView items列与列之间的间隔 - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section { return 1; } //items行与行之间的间隔 - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section { return 1; } #pragma mark - UICollectionViewDelegate //UICollectionView被选中时调用的方法 -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"选择第%ld章%ld个cell",(long)indexPath.section,(long)indexPath.row); } //返回这个UICollectionView是否可以被选择 -(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath { return YES; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } @end
相关文章推荐
- UI之textField、Button的创建方法及各属性
- Java 中 StringBuilder 在高性能用法总结
- Implement Queue using Stacks
- hdu 1005 Number Sequence(矩阵连乘+二分快速求幂)
- UI之苹果各种屏的屏幕大小及分辨率
- duilib获取CSliderUI滑动事件 - 用自定义控件实现
- UISegmentedControl
- RequireJS进阶(三) 转
- RequireJS进阶(二) 转
- RequireJS进阶(一) 转
- RequireJS 2.0 正式发布(转)
- UI基础-集合视图
- UIDatePicker的简单使用
- 【SSH项目实战】国税协同平台-19.信息发布管理完善&ueditor文本编辑插件
- UI:SwitchButton,可滑动的button
- RequireJS入门(三)转
- UISlider
- ios build时,Undefined symbols for architecture xxx问题
- String、StringBuffer与StringBuilder差分
- Programming Hive 第六章HiveQL:Queries笔记