蓝懿ios技术交流和心得分享16.1.4

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

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

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

  苹果官方Demo:点我下载

[objc] view plain

copy

  1. - (void)viewDidLoad  
  • {  
  •     [super

 viewDidLoad

];  

  •     self

.title

 = 

@"UICollectionView学习"

;  

  •       
  •     //通过Nib生成cell,然后注册 Nib的view需要继承 UICollectionViewCell

  

  •     [self

.collectionView

 registerNib

:[UINib

 nibWithNibName

:

@"SQCollectionCell"

 bundle

:nil

]

 forCellWithReuseIdentifier

:kcellIdentifier];  

  •       
  •     //注册headerView Nib的view需要继承UICollectionReusableView

  

  •     [self

.collectionView

 registerNib

:[UINib

 nibWithNibName

:

@"SQSupplementaryView"

 bundle

:nil

]

 forSupplementaryViewOfKind

:UICollectionElementKindSectionHeader

 withReuseIdentifier

:kheaderIdentifier];  

  •     //注册footerView Nib的view需要继承UICollectionReusableView

  

  •     [self

.collectionView

 registerNib

:[UINib

 nibWithNibName

:

@"SQSupplementaryView"

 bundle

:nil

]

 forSupplementaryViewOfKind

:UICollectionElementKindSectionFooter

 withReuseIdentifier

:kfooterIdentifier];  

  •     //

  

  •     self

.collectionView

.allowsMultipleSelection

 = 

YES

;

//默认为NO,是否可以多选

  

  •       
  • }  
  •   
  • - (void

)didReceiveMemoryWarning  

  • {  
  •     [super

 didReceiveMemoryWarning

];  

  •     // Dispose of any resources that can be recreated.

  

  • }  
  • #pragma mark -CollectionView datasource

  

  • //section

  

  • - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView

 *)collectionView  

  • {  
  •     return

 

2

;  

  • }  
  • //item个数

  

  • - (NSInteger)collectionView:(UICollectionView

 *)collectionView

 numberOfItemsInSection

:(NSInteger)section  

  • {  
  •     return

 

6

;  

  •       
  • }  
  •   
  • // The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:

  

  • - (UICollectionViewCell

 *)collectionView:(

UICollectionView

 *)collectionView

 cellForItemAtIndexPath

:(

NSIndexPath

 *)indexPath  

  • {  
  •     //重用cell

  

  •     UICollectionViewCell

 *cell = [collectionView

 dequeueReusableCellWithReuseIdentifier

:kcellIdentifier

 forIndexPath

:indexPath];  

  •     //赋值

  

  •     UIImageView

 *imageView = (

UIImageView

 *)[cell

 viewWithTag

:

1

];  

  •     UILabel

 *label = (

UILabel

 *)[cell

 viewWithTag

:

2

];  

  •     NSString

 *imageName = [NSString

 stringWithFormat

:

@"%ld.JPG"

,(

long

)indexPath

.row

];  

  •     imageView.image

 = [UIImage

 imageNamed

:imageName];  

  •     label.text

 = imageName;  

  •       
  •     cell.backgroundColor

 = [UIColor

 redColor

];  

  •     return

 cell;  

  •       
  • }  
  • // The view that is returned must be retrieved from a call to -dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:

  

  • - (UICollectionReusableView

 *)collectionView:(

UICollectionView

 *)collectionView

 viewForSupplementaryElementOfKind

:(

NSString

 *)kind

 atIndexPath

:(

NSIndexPath

 *)indexPath{  

  •       
  •     NSString

 *reuseIdentifier;  

  •     if

 ([kind

 isEqualToString

: UICollectionElementKindSectionFooter ]){  

  •         reuseIdentifier = kfooterIdentifier;  
  •     }else

{  

  •         reuseIdentifier = kheaderIdentifier;  
  •     }  
  •       
  •     UICollectionReusableView

 *view =  [collectionView dequeueReusableSupplementaryViewOfKind :kind  

 withReuseIdentifier

:reuseIdentifier  

 forIndexPath

:indexPath];  

  •       
  •     UILabel

 *label = (

UILabel

 *)[view

 viewWithTag

:

1

];  

  •     if

 ([kind

 isEqualToString

:UICollectionElementKindSectionHeader]){  

  •         label.text

 = [NSString

 stringWithFormat

:

@"这是header:%d"

,indexPath

.section

];  

  •     }  
  •     else

 

if

 ([kind

 isEqualToString

:UICollectionElementKindSectionFooter]){  

  •         view.backgroundColor

 = [UIColor

 lightGrayColor

];  

  •         label.text

 = [NSString

 stringWithFormat

:

@"这是footer:%d"

,indexPath

.section

];  

  •     }  
  •     return

 view;  

  • }  
  • //定义每个UICollectionViewCell 的大小

  

  • - (CGSize)collectionView:(UICollectionView

 *)collectionView

 layout

:(UICollectionViewLayout*)collectionViewLayout

 sizeForItemAtIndexPath

:(

NSIndexPath

 *)indexPath  

  • {  
  •     return

 CGSizeMake(

6

0

8

0

);  

  • }  
  • //定义每个Section 的 margin

  

  • -(UIEdgeInsets)collectionView:(UICollectionView

 *)collectionView

 layout

:(

UICollectionViewLayout

 *)collectionViewLayout

 insetForSectionAtIndex

:(NSInteger)section  

  • {  
  •     return

 UIEdgeInsetsMake(

1

5

1

5

5

1

5

);

//分别为上、左、下、右

  

  • }  
  • //返回头headerView的大小

  

  • -(CGSize)collectionView:(UICollectionView

 *)collectionView

 layout

:(

UICollectionViewLayout

 *)collectionViewLayout

 referenceSizeForHeaderInSection

:(NSInteger)section{  

  •     CGSize size={3

2

0

,

4

5

};  

  •     return

 size;  

  • }  
  • //返回头footerView的大小

  

  • - (CGSize)collectionView:(UICollectionView

 *)collectionView

 layout

:(UICollectionViewLayout*)collectionViewLayout

 referenceSizeForFooterInSection

:(NSInteger)section  

  • {  
  •     CGSize size={3

2

0

,

4

5

};  

  •     return

 size;  

  • }  
  • //每个section中不同的行之间的行间距

  

  • - (CGFloat)collectionView:(UICollectionView

 *)collectionView

 layout

:(UICollectionViewLayout*)collectionViewLayout

 minimumLineSpacingForSectionAtIndex

:(NSInteger)section  

  • {  
  •     return

 

1

0

;  

  • }  
  • //每个item之间的间距

  

  • //- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section

  

  • //{

  

  • //    return 100;

  

  • //}

  

  • //选择了某个cell

  

  • - (void

)collectionView:(

UICollectionView

 *)collectionView

 didSelectItemAtIndexPath

:(

NSIndexPath

 *)indexPath  

  • {  
  •     UICollectionViewCell

 *cell = [collectionView

 cellForItemAtIndexPath

:indexPath];  

  •     [cell setBackgroundColor

:[UIColor

 greenColor

]];  

  • }  
  • //取消选择了某个cell

  

  • - (void

)collectionView:(

UICollectionView

 *)collectionView

 didDeselectItemAtIndexPath

:(

NSIndexPath

 *)indexPath  

  • {  
  •     UICollectionViewCell

 *cell = [collectionView

 cellForItemAtIndexPath

:indexPath];  

  •     [cell setBackgroundColor

:[UIColor

 redColor

]];  

学习ios  重要还是要理清楚思路  在做或者看老师代码的时候 自己多想想为什么  不要自己看着就抄       另外还是要推荐一下 蓝懿IOS这个培训机构  和刘国斌老师刘国斌老师还是很有名气的,听朋友说刘老师成立了蓝懿iOS,,老师讲课方式很独特,能够尽量让每个人都能弄明白,有的比较难懂的地方,如果有的地方还是不懂得话,老师会换个其它方法再讲解,这对于我们这些学习iOS的同学是非常好的,多种方式的讲解会理解得更全面,这个必须得给个赞,嘻嘻,还有就是这里的学习环境很好,很安静,可以很安心的学习,安静的环境是学习的基础,小班讲课,每个班20几个学生,学习氛围非常好,每天都学到9点多才离开教室,练习的时间很充裕,而且如果在练习的过程中有什么困难,随时可以向老师求助,不像其它机构,通过视频教学,有的甚至学完之后都看不到讲师本人,问点问题都不方便,这就是蓝懿与其它机构的区别,相信在刘国斌老师的细心指导下,每个蓝懿学员都能找到满意的工作,加油!

                                                                  写博客第八十六天;

                                                                              QQ:565803433


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值