IOS中UICollectionView的基本用法

 



本章經過先整體介紹UICollectionView及其經常使用方法,再結合一個實例,瞭解如何使用UICollectionView。 網絡

 

UICollectionView 和 UICollectionViewController 類是iOS6 新引進的API,用於展現集合視圖,佈局更加靈活,可實現多列布局,用法相似於UITableView 和 UITableViewController 類。 ide

使用UICollectionView 必須實現UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout這三個協議。 佈局

 

下面先給出經常使用到的一些方法。(只給出經常使用的,其餘的能夠查看相關API)  spa

  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. }  

 

 

 

下面經過一個例子具體介紹下。(例子來自網絡。可是是經過第三方得到的,沒法取得連接。還望見諒。) .net

 

iOS CollectionView的出現是一大福利,不再用用TableView來定義複雜的多欄表格了,用法與Table相似,只是Cell必須本身添加,無默認模式 代理

因爲CollectionView沒有默認的Cell佈局,因此通常仍是自定義方便又快捷 orm

1、自定義Cell 繼承

一、新建類CollectionCell繼承自UICollectionViewCell 事件

二、新建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。


2、定義UICollectionView;

一、拖動一個Collection View到指定ViewController的View上

二、連線dataSource和delegate,並建立映射,命名爲CollectionView

三、選中CollectionView的標尺,將Cell Size的Width和Height改爲與自定義的Cell同樣的95*116,圖8

    

四、選中CollectionView的屬性,能夠修改其屬性,好比是垂直滑動,仍是水平滑動,選擇Vertical或Horizontal

五、選中CollectionViewCell,修改Class,繼承自CollectionCell

五、在ViewDidLoad方法中聲明Cell的類,在ViewDidLoad方法中添加,此句不聲明,將沒法加載,程序崩潰

其中,CollectionCell是這個Cell的標識(以前幾步已經定義過了。 ) 

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


六、在ViewController.h中聲明代理 

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


 

七、在.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  

便可,不贅述

相關文章
相關標籤/搜索