iOS开发- UICollectionView详解+实例

来源:互联网 发布:pdg2pdf for mac 编辑:程序博客网 时间:2024/05/18 01:43

文章来源:http://www.cnblogs.com/ios8/p/iOS-UICollectionView.html

本章通过先总体介绍UICollectionView及其常用方法,再结合一个实例,了解如何使用UICollectionView。

 

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

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

 

下面先给出常用到的一些方法。(只给出常用的,其他的可以查看相关API) 

  1. #pragma mark -- UICollectionViewDataSource   
  1. //定义展示的UICollectionViewCell的个数  
  2. -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section  
  3. {  
  4.     return 30;  
  5. }   
  1. //定义展示的Section的个数  
  2. -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView  
  3. {  
  4.     return 1;  
  5. }   
  1. //每个UICollectionView展示的内容  
  2. -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath  
  3. {  
  4.     static NSString * CellIdentifier = @"GradientCell";  
  5.     UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];  
  6.   
  7.     cell.backgroundColor = [UIColor colorWithRed:((10 * indexPath.row) / 255.0) green:((20 * indexPath.row)/255.0) blue:((30 * indexPath.row)/255.0) alpha:1.0f];  
  8.     return cell;  
  9. }   
  1. #pragma mark --UICollectionViewDelegateFlowLayout   
  1. //定义每个UICollectionView 的大小  
  2. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath  
  3. {  
  4.     return CGSizeMake(96, 100);  
  5. }   
  1. //定义每个UICollectionView 的 margin  
  2. -(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section  
  3. {  
  4.     return UIEdgeInsetsMake(5, 5, 5, 5);  
  5. }   
  1. #pragma mark --UICollectionViewDelegate   
  1. //UICollectionView被选中时调用的方法  
  2. -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath  
  3. {  
  4.     UICollectionViewCell * cell = (UICollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];  
  5.     cell.backgroundColor = [UIColor whiteColor];  
  6. }   
  1. //返回这个UICollectionView是否可以被选择  
  2. -(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath  
  3. {  
  4.     return YES;  
  5. }  

 

 

 

下面通过一个例子具体介绍下。(例子来自网络。但是是通过第三方获得的,无法取得链接。还望见谅。)

 

iOS CollectionView的出现是一大福利,再也不用用TableView来定义复杂的多栏表格了,用法与Table类似,只是Cell必须自己添加,无默认模式

由于CollectionView没有默认的Cell布局,所以一般还是自定义方便又快捷

一、自定义Cell

1、新建类CollectionCell继承自UICollectionViewCell

2、新建Xib,命名为CollectionCell.xib

a.选中CollectionCell.xib删掉默认的View,从控件中拖一个Collection View Cell(图3)到画布中,设置大小为95*116

 

b.选中刚刚添加的Cell,更改类名为CollectionCell,如图4

c.在CollectionCell.xib的CollectionCell中添加一个ImageView和一个Label(图5)

d.创建映射, 图6,图7

e.选中CollectionCell.m , 重写init方法 

  1. - (id)initWithFrame:(CGRect)frame  
  2. {  
  3.     self = [super initWithFrame:frame];  
  4.     if (self)  
  5.     {  
  6.         // 初始化时加载collectionCell.xib文件  
  7.         NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"CollectionCell" owner:self options:nil];  
  8.           
  9.         // 如果路径不存在,return nil  
  10.         if (arrayOfViews.count < 1)  
  11.         {  
  12.             return nil;  
  13.         }  
  14.         // 如果xib中view不属于UICollectionViewCell类,return nil  
  15.         if (![[arrayOfViews objectAtIndex:0] isKindOfClass:[UICollectionViewCell class]])  
  16.         {  
  17.             return nil;  
  18.         }  
  19.         // 加载nib  
  20.         self = [arrayOfViews objectAtIndex:0];  
  21.     }  
  22.     return self;  
  23. }  


f.选中CollectionCell.xib 修改其identifier为CollectionCell。


二、定义UICollectionView;

1、拖动一个Collection View到指定ViewController的View上

2、连线dataSource和delegate,并创建映射,命名为CollectionView

3、选中CollectionView的标尺,将Cell Size的Width和Height改成与自定义的Cell一样的95*116,图8

    

4、选中CollectionView的属性,可以修改其属性,比如是垂直滑动,还是水平滑动,选择Vertical或Horizontal

5、选中CollectionViewCell,修改Class,继承自CollectionCell

5、在ViewDidLoad方法中声明Cell的类,在ViewDidLoad方法中添加,此句不声明,将无法加载,程序崩溃

其中,CollectionCell是这个Cell的标识(之前几步已经定义过了。 ) 

  1. [self.collectionView registerClass:[CollectionCell class] forCellWithReuseIdentifier:@"CollectionCell"];  


6、在ViewController.h中声明代理 

  1. @interface ViewController : UIViewController<UICollectionViewDataSource,UICollectionViewDelegate>  



 

7、在.m文件中实现代理方法 

  1. //每个section的item个数  
  2. -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section  
  3. {  
  4.     return 12;  
  5. }  
  6.   
  7. -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath  
  8. {  
  9.       
  10.     CollectionCell *cell = (CollectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionCell" forIndexPath:indexPath];  
  11.       
  12.     //图片名称  
  13.     NSString *imageToLoad = [NSString stringWithFormat:@"%d.png", indexPath.row];  
  14.     //加载图片  
  15.     cell.imageView.image = [UIImage imageNamed:imageToLoad];  
  16.     //设置label文字  
  17.     cell.label.text = [NSString stringWithFormat:@"{%ld,%ld}",(long)indexPath.row,(long)indexPath.section];  
  18.       
  19.     return cell;  
  20. }  



 

8 。效果如图10

点击某项后跳转事件与UITableView类似,实现代理方法 

  1. -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath  

即可,不赘述

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 华为4x内存小怎么办 发现手机被用过怎么办 华为p8手机声音小怎么办 手机指纹解锁密码忘了怎么办 华为p10黑色掉漆怎么办 金立手机导航信号弱怎么办 手机导航时gps信号弱怎么办 三星手机导航gps信号弱怎么办 小米5导航信号弱怎么办 华为手机导航gps信号弱怎么办 手机屏玻璃裂了怎么办 oppo音量键坏了怎么办 魅蓝手机屏幕碎了怎么办 手机钢化膜边缘有气泡怎么办 手机贴钢化膜边缘有气泡怎么办 苹果手机钢化膜边缘有气泡怎么办 手机膜里的水泡怎么办 膜贴好了有气泡怎么办 后档玻璃膜气泡怎么办 贴膜里面有灰尘怎么办 贴膜边缘不粘怎么办 贴膜时候的气泡怎么办 贴手机膜起泡了怎么办 手机膜起泡泡了怎么办 手机膜鼓起来了怎么办 贴膜中间有气泡怎么办 新车贴膜有气泡怎么办 贴手机膜进灰尘怎么办 贴手机膜有灰尘怎么办 华为mate10后机壳碎了怎么办 手机屏和壳分开了怎么办 oppoa33手机开不了机怎么办 7p背面刮花了怎么办 oppo手机左右两键失灵怎么办 苹果5s屏幕翘起怎么办 0pp0手机有点卡怎么办 手机用久有点卡怎么办 手机有点卡怎么办魅蓝 苹果6排线接反了怎么办 oppo手机解锁密码忘了怎么办 魅蓝5音量小怎么办