#pragma marc 添加Collection -(void)addConllectionView{ //collection的佈局方案 UICollectionViewFlowLayout *collectionViewLayout=[[UICollectionViewFlowLayout alloc]init]; //設置位置大小以及佈局方案 _myCollectionView=[[UICollectionView alloc]initWithFrame:CM(0, 0, VIEW_WIDTH,VIEW_HEIGHT-64-48) collectionViewLayout:collectionViewLayout]; //CollectionView的背景顏色 _myCollectionView.backgroundColor=[UIColor whiteColor]; [self addSubview:_myCollectionView]; //設置代理 _myCollectionView.delegate=self; _myCollectionView.dataSource=self; //註冊Cell 以及選擇控制的類 [_myCollectionView registerClass:[ExhibitionCollectionViewCell class] forCellWithReuseIdentifier:@"cell"]; //註冊Head以及Food 以及選擇控制的類 [_myCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Head"]; } #pragma mark 設置Cell -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ ExhibitionCollectionViewCell *cell=[_myCollectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath]; cell.backgroundColor=[UIColor whiteColor]; cell.imageView.image=[UIImage imageNamed:@"商品"]; return cell; } #pragma mark 設置頭部以及尾部 - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { if ([kind isEqualToString:UICollectionElementKindSectionHeader]) { //和判斷複用同樣 UICollectionReusableView * head = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Head" forIndexPath:indexPath]; //添加輪播圖 [head addSubview:_carouselFigureView]; return head; } return nil; } #pragma mark 設置Cell返回數量 -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ return 20; } #pragma mark 定義展現的組數 -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } #pragma mark 設置cell的大小 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { return CGSizeMake(VIEW_WIDTH/3.55f, VIEW_HEIGHT/3.550f); } #pragma mark 設置每一個section中不一樣的行之間的行間距 - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section { return 20; } #pragma mark 設置每一個section中不一樣的列之間的間距 - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section { return 10; } #pragma mark 設置每一個Cell的內邊距 -(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { return UIEdgeInsetsMake(0, 0, 10, 0);//分別爲上、左、下、右 } #pragma mark 點擊事件 - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ CommodityDisplayControllerView * CDVC=[[CommodityDisplayControllerView alloc]init]; [[self getController] presentViewController:CDVC animated:YES completion:nil]; NSLog(@"%ld",indexPath.row); } #pragma mark 返回頭headerView的大小 -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{ CGSize size={VIEW_WIDTH,IMG_EXHIBITION_HEIGHT}; return size; }