#import "ViewController.h" #define kScreenSize [UIScreen mainScreen].bounds.size //遵照協議 @interface ViewController () <UICollectionViewDataSource,UICollectionViewDelegate> { UICollectionView *_collectionView; } @property (nonatomic,strong) UICollectionView *collectionView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self creatCollectionView]; } - (void)creatCollectionView { //1.先建立一個 UICollectionViewFlowLayout (是集合視圖的核心) //經過UICollectionViewFlowLayout 來進行佈局 UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; //2.對layout 作一些基本的設置 //設置cell 的大小(全局設置)(若是要單個對cell 大小進行設置須要遵照UICollectionViewDelegateFlowLayout協議實現方法) layout.itemSize = CGSizeMake(100, 100); //設置滾動的方向(水平和豎直) //UICollectionViewScrollDirectionHorizontal水平 //UICollectionViewScrollDirectionVertical layout.scrollDirection = UICollectionViewScrollDirectionVertical; //設置 cell 之間的最小豎直間隔 (上下cell間隔) layout.minimumLineSpacing = 30; //cell 之間的 最小水平間隔(水平方向的cell 間隔) layout.minimumInteritemSpacing = 10; //設置分區頭視圖的大小 (若是是豎直滾動那麼設置height有效,width無效,水平滾動那麼width 有效) //分區頭間隔 layout.headerReferenceSize = CGSizeMake(50, 50); //分區尾間隔(實際上就是尾視圖的高) layout.footerReferenceSize = CGSizeMake(50, 50); //設置整個分區中全部cell(總體) 距離分區的上左下右的間距 layout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20); //實例化一個 集合視圖對象 經過layout 佈局 self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 20, kScreenSize.width, kScreenSize.height-20) collectionViewLayout:layout]; //設置代理和數據源對象 self.collectionView.dataSource = self; self.collectionView.delegate = self; //cell 必須提早註冊 複用標誌要和下面隊列獲取cell 的時候同樣 [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"CollectionViewCell"]; //若是設置分區的頭尾視圖那麼須要進行註冊(註冊以後就能夠採用複用隊列) //kind 表示 是頭視圖仍是尾視圖 第三個參數就是一個複用標識符 要和下面使用保持一致 [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header"]; //註冊分區尾視圖 [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"Footer"]; [self.view addSubview:self.collectionView]; self.view.backgroundColor = [UIColor lightGrayColor]; } #pragma mark UICollectionViewDataSource delegate //有多少分區 - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 2; } //每一個分區有多少個cell - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return 25; } //獲取指定分區內的cell - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellId = @"CollectionViewCell"; //從複用隊列獲取 cell (須要提早註冊) UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath]; //設置背景顏色 cell.backgroundColor = [UIColor yellowColor]; return cell; } #pragma mark - 獲取分區頭尾視圖 //建立 分區頭尾視圖調用 - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { //建立/獲取頭尾視圖的時候也要用複用隊列 //判斷kind 來區分頭尾 if ([kind isEqualToString:UICollectionElementKindSectionHeader]) { //頭視圖 //(若是知足不了須要那麼就定製) UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"Header" forIndexPath:indexPath]; header.backgroundColor = [UIColor greenColor]; return header; }else { //尾視圖 //複用隊列獲取 UICollectionReusableView *footer = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"Footer" forIndexPath:indexPath]; footer.backgroundColor = [UIColor yellowColor]; return footer; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end