您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS UICollectionView 使用綜述(一):基礎篇——垂直列表與橫向相冊

編輯:iOS 開發綜合

效果展示

一、簡述

這裡的效果是使用 UICollectionView 來實現的,透過它能輕鬆做出垂直列表與橫向相冊等多種排列方式。

二、storyboard 搭配自訂 Cell 的方式實現

基本思路是:使用 storyboard 進行布局,搭配自訂的 CellView 來展示資料。

2.1 首先在 storyboard 中定義內容

我們在主視圖中直接拖入一個 UICollectionView 控件。它其實就像一個容器,同時會被預設套用一種流水式的布局方式:UICollectionViewFlowLayout。接著在對應的主視圖控制器中,分別為 UICollectionView 與 UICollectionViewFlowLayout 建立引用。

2.2 自訂 CellView 展示條目

建立 AppCell.h 檔案,繼承自 UICollectionViewCell,並在 AppCell.m 中實作初始化方法:

#import "AppCell.h"

@implementation AppCell
-(instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        // 1、取得 Cell 的 Size
        CGSize cellSize = self.contentView.frame.size;
        // 2、新增一個顯示標題的 Label
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, cellSize.width, 20)];
        titleLabel.text = @"神奇的小鎮";
        [self.contentView addSubview:titleLabel];
        // 3、新增一個 ImageView 顯示圖片
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 25, cellSize.width, cellSize.height - 25)];
        imageView.image = [UIImage imageNamed:@"aqs"];
        [self.contentView addSubview:imageView];
    }
    return self;
}
@end

可以看到,在這個自訂 Cell 中加入了一個用於顯示圖片的 UIImageView 與一個顯示文字標題的 UILabel。也就是說,當我們的條目內容比較複雜時,就可以用這種方式來添加與佈局。

2.3 設定視圖顯示

#import "ViewController.h"
#import "AppCell.h"

@interface ViewController () 
// 對應 storyboard 中的 UICollectionView
@property (weak, nonatomic) IBOutlet UICollectionView *mainCollectionView;
// 對應 storyboard 中 UICollectionView 的流水布局
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *mainFlowLayout;
@end

// 重用標識符
static NSString *ident = @"cell";

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // 設定代理
    self.mainCollectionView.dataSource = self;
    // 註冊 Cell
    [self.mainCollectionView registerClass:[AppCell class] forCellWithReuseIdentifier:ident];
    // 設定 Item 的大小
    self.mainFlowLayout.itemSize = CGSizeMake(self.view.frame.size.width - 20, 200);
}

// 組
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

// 列
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 30;
}

// 視圖
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    AppCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ident forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[AppCell alloc] init];
    }
    cell.backgroundColor = [UIColor colorWithRed:248 green:248 blue:248 alpha:1];
    return cell;
}
@end

2.3.1 視圖引用

這裡定義了 mainCollectionView 與 mainFlowLayout 兩個引用,分別指向第一步中建立的 storyboard 裡的 UICollectionView 與其對應的流水布局 UICollectionViewFlowLayout。每一個 UICollectionView 必然對應一種布局方式,它指定了 Item 的排列方式,此處預設使用流水式布局。

2.3.2 UICollectionView 的相關設定

在 storyboard 中已預設了一個流水布局,若改用純程式碼建立,則必須明確定義一種布局方式,否則會拋出異常。同時也必須註冊對應的 Cell 條目類型,否則同樣會拋出異常。此外,若不設定 itemSize,條目會以預設的 50×50 顯示。

2.3.3 UICollectionView 的分組支援

UICollectionView 同時支援分組顯示功能,這一點與 UITableView 相似。

三、storyboard 搭配自訂 xib 的方式實現

接下來改用自訂 xib 的方式來實現列表展示,同樣以 storyboard 進行布局規劃。

3.1 自訂條目 Cell 對應的 XIB 檔

在 xib 檔中對應控制器 AppCell,分別規劃了一個顯示圖片的 UIImageView 與一個顯示標題的 UILabel,並在控制器中建立相應的控件引用。

#import "AppCell.h"

@interface AppCell ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@end

@implementation AppCell
- (void)awakeFromNib {
    [super awakeFromNib];
}

-(void)setImagPath:(NSString *)imagPath {
    _imagPath = imagPath;
    // 設定 ImageView
    self.imageView.image = [UIImage imageNamed:imagPath];
    // 設定顯示標題
    self.titleLabel.text = @"this is a text";
}
@end

這裡重寫了 imagPath 的 setter 方法,也就是說當外部呼叫 appCell.imagPath 時,會執行到 setImagPath 方法,並在其中把對應的資料設定到各控件上。

3.2 UICollectionView 的相關設定

#import "ViewController.h"
#import "AppCell.h"

@interface ViewController () 
@property (weak, nonatomic) IBOutlet UICollectionView *mainCollcetionView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *mainFlowLayout;
@end

static NSString *ident = @"cell";

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // 設定 Item 的大小
    self.mainFlowLayout.itemSize = CGSizeMake(self.view.frame.size.width - 20, 180);
    // 設定代理
    self.mainCollcetionView.dataSource = self;
    // 註冊 Cell
    UINib *cellNib = [UINib nibWithNibName:@"AppCell" bundle:nil];
    [self.mainCollcetionView registerNib:cellNib forCellWithReuseIdentifier:ident];
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 30;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    AppCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ident forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[AppCell alloc] init];
    }
    // 設定背景
    cell.backgroundColor = [UIColor colorWithRed:248 green:248 blue:248 alpha:1];
    // 設定顯示資料
    cell.imagPath = @"aqs";
    return cell;
}
@end

3.2.1:使用 UICollectionView 時必須對條目 CellView 進行註冊,這裡是先取得對應的 UINib,再呼叫其方法完成註冊。

3.2.2:在 cellForItemAtIndexPath 方法中,我們為 cell.imagPath 賦值,這會觸發 AppCell 對應的 setImagPath 方法,進而對相應控件進行賦值。相較於純程式碼建立 Cell,使用 xib 能讓介面佈局更加直觀,也更容易維護。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved