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

讓 UITableView「優雅」支持不同類型的 Cell

編輯:iOS 開發基礎

在某些業務場景下,同一個 UITableView 需要支持多種 UITableViewCell。想想即時通信的聊天對話列表,不同的消息類型對應不同的 UITableViewCell,同一個 tableview 往往需要支持多達 10 種以上的 cell 類型,例如文本、圖片、位置、紅包等等。一般情況下,UITableViewCell 往往會和業務數據模型綁定來展示數據,根據不同的業務數據對應不同的 cell。本文將探討如何有效地管理和加載多種類型的 cell。

為了方便討論,假設我們要實現一個員工管理系統。一個員工包含名字和頭像:如果員工只有名字,則只展示名字;如果只有頭像,則只展示頭像;如果既有名字又有頭像,則兩者都展示。

我們用一個 Person 類表示員工,用三種不同的 cell 來處理不同的展示情況。

@interface Person : NSObject @property (nonatomic, copy) NSString *name; @property (nonatomic, strong) NSString *avatar; @end
/*負責展示只有名字的員工*/ @interface TextCell : BaseCell - (void)setPerson:(Person *)p; @end
/*負責展示只有頭像的員工*/ @interface ImageCell : BaseCell - (void)setPerson:(Person *)p; @end
/*負責展示既有名字又有頭像的員工*/ @interface TextImageCell : BaseCell - (void)setPerson:(Person *)p; @end

這三個類都繼承了 BaseCell,而 BaseCell 繼承自 UITableViewCell。

@interface BaseCell : UITableViewCell - (void)setPerson:(Person *)p; @end

下面我們在 UITableView 的 delegate 中處理 Cell 展示。

第一次嘗試:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {     BaseCell *cell;     NSString *cellIdentifier;     switch (p.showtype) {         case PersonShowText:             cellIdentifier = @"TextCell";             break;         case PersonShowAvatar:             cellIdentifier = @"PersonShowAvatar";             break;         case PersonShowTextAndAvatar:             cellIdentifier = @"PersonShowTextAndAvatar";             break;         default:             break;     }     cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];     if (!cell) {         switch (p.showtype) {             case PersonShowText:                 cell = [[TextCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];                 break;             case PersonShowAvatar:                 cell = [[ImageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];                 break;             case PersonShowTextAndAvatar:                 cell = [[TextImageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];                 break;             default:                 break;         }     }     [cell setPerson:p];     return cell; }

這段程式碼實現了根據不同業務模型選取和顯示 Cell 的邏輯,但包含了重復程式碼——switch case 被調用了兩次。我們改進一下:

第二次嘗試

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {     BaseCell *cell;     NSString *cellIdentifier;     Class cellClass;     switch (p.showtype) {         case PersonShowText:             cellClass = [TextCell class];             break;         case PersonShowAvatar:             cellClass = [ImageCell class];             break;         case PersonShowTextAndAvatar:             cellClass = [TextImageCell class];             break;         default:             cellClass = [UITableViewCell class];             break;     }     cellIdentifier = NSStringFromClass(cellClass);     cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];     if (!cell) {         cell = [[cellClass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];     }     [cell setPerson:p];     return cell; }

這次比第一次好了不少,透過一個通用的 Class 物件動態生成 cell,避免了兩次調用 switch case 的重復程式碼。但,還有更好的實現方式嗎?

第三次嘗試

- (void)viewDidLoad  {     ...     [self registerCell];  //注冊cell }
- (void)registerCell {     [_tableView registerClass:[TextCell class] forCellReuseIdentifier:NSStringFromClass([TextCell class])];     [_tableView registerClass:[ImageCell class] forCellReuseIdentifier:NSStringFromClass([ImageCell class])];     [_tableView registerClass:[TextImageCell class] forCellReuseIdentifier:NSStringFromClass([TextImageCell class])]; }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {     Person *p = _persons[indexPath.row];     BaseCell *cell;     NSString *cellIdentifier;     switch (p.showtype) {         case PersonShowText:             cellIdentifier = NSStringFromClass([TextCell class]);             break;         case PersonShowAvatar:             cellIdentifier = NSStringFromClass([ImageCell class]);             break;         case PersonShowTextAndAvatar:             cellIdentifier = NSStringFromClass([TextImageCell class]);             break;         default:             cellIdentifier = NSStringFromClass([UITableViewCell class]);             break;     }     cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];     [cell setPerson:p];     return cell; }

可以看到,這次我們調用了 - (void)registerClass:(nullable Class)cellClass forCellReuseIdentifier:(NSString *)identifier 方法,把 tableView 和 cell 先配置好,並且在 cellForRowAtIndexPath 裡去掉了 if (!cell) {...} 的處理,程式碼看起來更加簡潔。

為什麼不再需要判斷 cell 是否為空?因為透過 registerClass 方法注冊 cell 之後,dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath 方法會確保有一個可用的 cell 返回。

當然,我們可以把類型判斷這段程式碼提取出來,讓 cellForRowAtIndexPath 看起來更加簡潔:

@interface Person : NSObject ...... @property (nonatomic, strong) NSString *cellIdentifier; @end
@implementation Person - (NSString *)cellIdentifier {     if (_showtype == PersonShowTextAndAvatar) {         return NSStringFromClass([TextImageCell class]);     } else if (_showtype == PersonShowAvatar){         return NSStringFromClass([ImageCell class]);     } else {         return NSStringFromClass([TextCell class]);     } } @end

現在 cellForRowAtIndexPath 方法看起來就像下面這樣,明顯簡潔多了:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {     Person *p = _persons[indexPath.row];     BaseCell *cell;     NSString *cellIdentifier = p.cellIdentifier;     cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];     [cell setPerson:p];     return cell; }

結論:

使用 - (void)registerClass:(nullable Class)cellClass forCellReuseIdentifier:(NSString *)identifier 和 - (UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath,可以讓 UITableView 處理多種類型的 cell 更加靈活和輕鬆。

文章轉自 la0fu的簡書

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