UICollectionView的基本使用


UICollectionView在目前的iOS開發中,使用非常廣泛。它繼承自UIScrollView,可以根據需要自定義各種各樣復雜的布局。

使用

  • 遵循兩個協議
    數據源協議UICollectionViewDataSource
    代理方法協議UICollectionViewDelegate

  • 注冊cell

1 [collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"cellID"];

 

  • 布局參數對象UICollectionViewFlowLayout
    這也是UICollectionView的精髓所在,正是通過它,我們才實現了UICollectionView各式各樣的布局。

系統提供了兩個UICollectionView的布局類:
1.UICollectionViewLayout
是一個抽象類,我們在自定義布局的時候可以繼承此類,並在此基礎上設置布局信息。
2.UICollectionViewFlowLayout
繼承於UICollectionViewLayout,是系統寫好的布局類,該類為我們提供了一個簡單的布局樣式。假如我們只需要一個特別簡單的網格布局或者流水布局,可以直接使用它。

創建

不能用init的方式,因為必須提供一個不為空的layout布局參數,給它布局。

1、創建流水布局layout

在其中設定相關屬性,如Item的大小等

 1 UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
 2 
 3     // 設置item的行間距和列間距
 4     layout.minimumInteritemSpacing = kLineSpacing;
 5     layout.minimumLineSpacing = kLineSpacing;
 6 
 7     // 設置item的大小
 8     CGFloat itemW = kScreenWidth / 2.5 ;
 9     layout.itemSize = CGSizeMake(itemW, itemW);
10 
11     // 設置每個分區的 上左下右 的內邊距
12     layout.sectionInset = UIEdgeInsetsMake(5, 5 ,5, 5);
13     
14     // 設置區頭和區尾的大小
15     layout.headerReferenceSize = CGSizeMake(kScreenWidth, 65);
16     layout.footerReferenceSize = CGSizeMake(kScreenWidth, 65);
17 
18     // 設置分區的頭視圖和尾視圖 是否始終固定在屏幕上邊和下邊
19     layout.sectionFootersPinToVisibleBounds = YES;
20 
21     // 設置滾動條方向
22     layout.scrollDirection = UICollectionViewScrollDirectionVertical;  

 

2、利用layout 創建collecView
 1  UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
 2     collectionView.backgroundColor = [UIColor witheColor];
 3     collectionView.showsVerticalScrollIndicator = NO;   //是否顯示滾動條
 4     collectionView.scrollEnabled = YES;  //滾動使能
 5 
 6     //3、添加到控制器的view
 7     [self.view addSubview:collectionView];
 8     _mainCollectionView = collectionView;
 9 
10     //4、布局
11     [_mainCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
12         make.top.left.bottom.right.equalTo(self.view);
13     }];

 

5、注冊cell

一般是自定義的cell。需要注冊不同的cell,可由不同的cell的ID決定。

1  [self.mainCollectionView registerClass:[PDcollectionVertivalCell class] 
2 forCellWithReuseIdentifier:@"MAINCOLLECTIONVIEWID"];
6、注冊頭部視圖
1 [self.mainCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader 
2 withReuseIdentifier:@"MAINCOLLECTIONVIEWHEADER"];
7、遵守協議,設置代理
1 <UICollectionViewDelegate,UICollectionViewDataSource>
2 
3     self.mainCollectionView.delegate = self;
4     self.mainCollectionView.dataSource = self;

 

數據源方法

 1 #pragma mark -collectionview 數據源方法
 2 - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
 3 
 4     return 6;   //返回section數
 5 }
 6 - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
 7 
 8     return self.normalGoodLists.count;  //每個section的Item數
 9 }
10 
11 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
12 {
13     創建item / 從緩存池中拿 Item
14     PDcollectionVertivalCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MAINCOLLECTIONVIEWID" forIndexPath:indexPath];
15     // 外界在此給Item添加模型數據
16     if(self.normalGoodLists.count > 0){
17         PDshopItem *item = self.normalGoodLists[indexPath.item];
18         cell.cheapShopItem = item;
19     }
20     return cell;
21     
22 }

 

創建頭部視圖

 1 #pragma mark - 頭部視圖
 2 -(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
 3     
 4     UICollectionReusableView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader 
 5 withReuseIdentifier:@"MAINCOLLECTIONVIEWHEADER" forIndexPath:indexPath];
 6     // ID必須與注冊ID一樣
 7    
 8     if(indexPath.section == 0){
 9         
10         [headView addSubview:self.cycleScrollView];  //任何自定義view
11         [self.cycleScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
12             make.top.left.bottom.right.equalTo(headView);
13         }];
14         return headView;
15     }
16 }
17 
18 #pragma mark - 頭部視圖的尺寸
19 -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
20     
21     if(section ==0 ){
22         
23         return CGSizeMake(screenW, 0.4*screenW);
24     }
25     else if(section ==1 ){
26         
27         return CGSizeMake(screenW, 0.25253*screenW);
28     }
29 }

 

代理方法--點擊事件

1 #pragma mark - 點擊 某個Item時 調用
2 - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
3     [collectionView deselectItemAtIndexPath:indexPath animated:YES];//取消選中
4     //do something ...
5 }    

 

UICollectionViewCell

UICollectionView的item,就是一個cell。

  • cell的內容——自定義
    默認的cell是UICollectionViewCell對象,它沒有任何子控件,所以必須自定義cell(繼承自UICollectionViewCell),添加所需的子控件。

  • cell的屬性——由flowlayout決定,在創建流水布局參數時設定好。

自定義cell

繼承自UICollectionViewCell,在.h文件中,添加數據模型屬性,用於后續設置cell的內容

1 #import <UIKit/UIKit.h>
2 #import "PDshopItem.h"
3 
4 
5 @interface PDcollectionViewCell : UICollectionViewCell
6 
7 @property(nonatomic,strong)PDshopItem *shopItem;    //數據模型
8 
9 @end

 

在.m文件中

 1 @interface PDcollectionViewCell()
 2 
 3 @property(nonatomic,weak)UIImageView *shopImgView;
 4 @property(nonatomic,weak)UILabel *shopNameLabel;
 5 
 6 @end
 7 
 8 // 在init方法中添加子控件
 9 -(id)initWithFrame:(CGRect)frame{
10 
11     if([super initWithFrame:frame]){
12     //創建子控件、布局
13     ... ... 
14     }
15     return self;
16 }
17 
18 // 重寫屬性的set方法,給子控件設置數據
19 -(void)setShopItem:(PDshopItem *)shopItem{
20     _shopItem = shopItem;
21     
22     self.shopNameLabel.text = shopItem.goodName;
23     ... ...
24  }

 

 

摘自:https://www.jianshu.com/p/a5f71dc1ead3


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM