pragma mark - 創(chuàng)建UI
-(void)createUI
{
// 創(chuàng)建網格
布局對象 UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc]init];
//設置滾動方向
flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
//創(chuàng)建網格對象
_collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H) collectionViewLayout:flowLayout];
//設置背景色,系統(tǒng)默認的是黑色的
_collectionView.backgroundColor = [UIColor whiteColor];
//設置代理
_collectionView.delegate = self; _collectionView.dataSource = self; [self.view addSubview:_collectionView];
//注冊cell類
[_collectionView registerClass:[MusicCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
//注冊header類
[_collectionView registerClass:[MusicCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"view"];
//注冊footer類
[_collectionView registerClass:[MusicCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"view"];
}
pragma mark - 實現代理方法
//確定section的個數
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; }
//確定每個section對應的item的個數
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.imageArray.count; }
//創(chuàng)建cell
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { MusicCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath]; //賦值 [cell.imageView setImage:[UIImage imageNamed:self.imageArray[indexPath.item]]]; cell.titleLabel.text = self.nameArray[indexPath.item]; return cell; }
//設置item的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { return CGSizeMake((SCREEN_W - 20) / 2, 150); }
//設置垂直間距,默認的垂直和水平間距都是10
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section { return 5; }
//設置水平間距
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section { return 5; }
//四周的邊距
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { return UIEdgeInsetsMake(5, 5, 5, 5); }
//設置header的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section { return CGSizeMake(60, 30); }
//設置footer的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section { return CGSizeMake(60, 30); }
//設置header和footer的view
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { MusicCollectionReusableView * view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"view" forIndexPath:indexPath]; //分別給header和footer賦值 if (kind == UICollectionElementKindSectionHeader) { view.titleLabel.text = @"段頭"; } else { view.titleLabel.text = @"段尾"; } return view; }
設置選中
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { MusicDetailViewController * vc = [[MusicDetailViewController alloc]init]; //傳值 vc.typeString = self.nameArray[indexPath.item]; vc.urlString = self.urlArray[indexPath.item]; vc.hidesBottomBarWhenPushed = YES; [self.navigationController pushViewController:vc animated:YES];
}