//建立佈局 UICollectionViewFlowLayout * danPinFlowLayout =[[UICollectionViewFlowLayout alloc]init]; danPinFlowLayout.itemSize= CGSizeMake(230, 240); danPinFlowLayout.headerReferenceSize=CGSizeMake(0, 50); danPinFlowLayout.footerReferenceSize =CGSizeMake(0, 50); danPinFlowLayout.scrollDirection=UICollectionViewScrollDirectionVertical;//豎向滾動 //初始化collectionView CGRect collectRect=CGRectMake(0, 50, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)); if(_DanPinCollectionView ==nil){ _DanPinCollectionView =[[UICollectionView alloc]initWithFrame:collectRect collectionViewLayout:danPinFlowLayout]; _DanPinCollectionView.tag=200; _DanPinCollectionView.backgroundColor=[UIColor whiteColor]; _DanPinCollectionView.dataSource=self; _DanPinCollectionView.delegate=self; _DanPinCollectionView.showsVerticalScrollIndicator = NO; [self.view addSubview:_DanPinCollectionView]; //註冊cell UINib *nibCell = [UINib nibWithNibName:@"DanPinCollectionViewCell" bundle: [NSBundle mainBundle]]; [_DanPinCollectionView registerNib:nibCell forCellWithReuseIdentifier:@"danPinCellIdentifier"]; //headerView註冊 [_DanPinCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderIdentifierhead"]; //footView註冊 [_DanPinCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FootIdentifierhead"]; } }
cell模板建議使用xib文件直接建立,更快捷方便。dom
基礎協議實現佈局
#pragma mark ----------------UIcollectionViewDataSource--------------- //組的個數 - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } //單元格的個數 - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return 20;//self.DanPinDataSource.count; } //構建單元格 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"danPinCellIdentifier" forIndexPath:indexPath]; cell.backgroundColor = [UIColor whiteColor]; return cell; } //組的頭視圖建立 - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { if(kind ==UICollectionElementKindSectionHeader){ UICollectionReusableView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"HeaderIdentifierhead" forIndexPath:indexPath]; headView.backgroundColor = [UIColor blueColor]; return headView; } else{ UICollectionReusableView *footView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"FootIdentifierhead" forIndexPath:indexPath]; footView.backgroundColor = [UIColor redColor]; return footView; } } //經過協議方法設置單元格尺寸 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { // CGFloat rd = arc4random()%200; return CGSizeMake(220, 240); }