UICollectionView 可以用 Storyboard 拉,也可以全用程式碼寫。後者雖然囉嗦些,但版面完全可控,也比較適合需要動態計算尺寸的場景。以下走一遍完整流程。
在工程中新增檔案,選擇「Cocoa Touch Class」,父類別選 UICollectionViewCell,命名為 CollectionViewCell。
// 圖片
@property (strong, nonatomic) UIImageView *fruitImage;
// 標題
@property (strong, nonatomic) UILabel *fruitTitle;
// 價格
@property (strong, nonatomic) UILabel *priceLabel;
- (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;
}
- (void)creatCollectionView
{
// 1. 初始化 layout
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
// layout.scrollDirection = UICollectionViewScrollDirectionHorizontal; // 改為橫向捲動
// layout.headerReferenceSize = CGSizeMake(kScreenWidth, 100); // 也可在此設 itemSize
// 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. 註冊 cell(ReuseIdentifier 必須與 dequeue 時一致)
[mainCollectionView registerClass:[FruitCollectionViewCell class]
forCellWithReuseIdentifier:@"cellId"];
// 4. 設定代理
mainCollectionView.delegate = self;
mainCollectionView.dataSource = self;
}
#pragma mark collectionView 代理方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (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);
}
// 內距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(0, 2, 0, 2);
}
// 水平間距
- (CGFloat)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
return 2;
}
// 垂直間距
- (CGFloat)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return 5;
}
// 點擊
- (void)collectionView:(UICollectionView *)collectionView
didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
FruitCollectionViewCell *cell = (FruitCollectionViewCell *)
[collectionView cellForItemAtIndexPath:indexPath];
NSLog(@"%@", cell.fruitTitle.text);
}
若需要區段標頭,必須先註冊 UICollectionReusableView,再利用 viewForSupplementaryElementOfKind:atIndexPath: 取出;此處的 ReuseIdentifier 要與註冊時完全相同,否則會在執行期崩潰。範例中這段已預留,取消註解即可使用。
另外,上面所有間距方法都屬於 UICollectionViewDelegateFlowLayout,別忘了讓你的控制器遵循這個額外的協定,否則方法根本不會被呼叫——這是純程式碼建置時最容易漏掉的一步。