1.創建自定義UICollectionViewCell
選中工程,右鍵-New File…選擇“Cocoa Touch Class”-Next,選擇繼承於UICollectionViewCell類,給個合理的名稱CollectionViewCell,再Next完成。
1創建
在UICollectionViewCell中定義你所需要的控件
//圖片
@property(strong,nonatomic)UIImageView *fruitImage;
//標題
@property(strong,nonatomic)UILabel *fruitTitle;
//價格
@property(strong,nonatomic)UILabel *priceLabel;
在UICollectionViewCell.m文件中重寫
-(instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
_fruitImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth/2-1 , kScreenHeight/3.4)];
_fruitImage.image = [UIImage imageNamed:@"pic_fruit"];
[self.contentView addSubview:_fruitImage];
_fruitTitle = [[UILabel alloc]initWithFrame:CGRectMake(7, kScreenHeight/3.4 , kScreenWidth/2-6, 50)];
_fruitTitle.numberOfLines = 0;
_fruitTitle.font = [UIFont systemFontOfSize:13];
_fruitTitle.textColor = [UIColor grayColor];
[self.contentView addSubview:_fruitTitle];
}
return self;
}
2.在ViewController.m文件中創建UICollectionView實例
//創建collectionView
-(void)creatCollectionView{
//1.初始化layout
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
//設置collectionView滾動方向
// [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
//設置headerView的尺寸大小
// layout.headerReferenceSize = CGSizeMake(kScreenWidth, 100);
//該方法也可以設置itemSize
// layout.itemSize =CGSizeMake(0, 150);
//2.初始化collectionView
mainCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, kScreenHeight /1.1, kScreenWidth, kScreenHeight*1.6) collectionViewLayout:layout];
[self.upDownScrollView addSubview:mainCollectionView];
mainCollectionView.backgroundColor = [UIColor colorWithRed:235/255.0 green:235/255.0 blue:235/255.0 alpha:1];
//3.注冊collectionViewCell
//注意,此處的ReuseIdentifier 必須和 cellForItemAtIndexPath 方法中 一致 均為 cellId
[mainCollectionView registerClass:[FruitCollectionViewCell class] forCellWithReuseIdentifier:@"cellId"];
// //注冊headerView 此處的ReuseIdentifier 必須和 cellForItemAtIndexPath 方法中 一致 均為reusableView
// [mainCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reusableView"];
//4.設置代理
mainCollectionView.delegate = self;
mainCollectionView.dataSource = self;
}
3.實現UICollectionView代理方法
#pragma mark collectionView代理方法
//返回section個數
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
//每個section的item個數
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 6;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
FruitCollectionViewCell *cell = (FruitCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cellId" forIndexPath:indexPath];
if (indexPath.section == 0 && indexPath.row == 1 ) {
cell.fruitTitle.text = @"正宗台灣產高雄特產熱帶香蕉好吃包郵123";
}else{
cell.fruitTitle.text = @"火爆進口馬來西亞特產大金龍芒果包郵500g";
}
cell.backgroundColor = [UIColor whiteColor];
return cell;
}
//設置每個item的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(kScreenWidth/2-4, kScreenHeight/2.5);
}
//footer的size
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
//{
// return CGSizeMake(10, 10);
//}
//header的size
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
//{
// return CGSizeMake(10, 10);
//}
//設置每個item的UIEdgeInsets
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(0, 2, 0, 2);
}
//設置每個item水平間距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
return 2;
}
//設置每個item垂直間距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return 5;
}
//通過設置SupplementaryViewOfKind 來設置頭部或者底部的view,其中 ReuseIdentifier 的值必須和 注冊是填寫的一致,本例都為 “reusableView”
//- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
//{
// UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reusableView" forIndexPath:indexPath];
// headerView.backgroundColor =[UIColor grayColor];
// UILabel *label = [[UILabel alloc] initWithFrame:headerView.bounds];
// label.text = @"這是collectionView的頭部";
// label.font = [UIFont systemFontOfSize:20];
// [headerView addSubview:label];
// return headerView;
//}
//點擊item方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
FruitCollectionViewCell *cell = (FruitCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
NSString *msg = cell.fruitTitle.text;
NSLog(@"%@",msg);
}
看到這裡相信你已經掌握了UICollectionView的使用方法啦