封裝的tableView,瘦身VC

導(dǎo)語:

通常我們?cè)陂_發(fā)中,90%的時(shí)間都是在寫一些重復(fù)的垃圾代碼,真正的寫代碼時(shí)間其實(shí)只有5%左右的時(shí)間,而tableView卻是我們最常用的控件,也是復(fù)用率最高的控件,那么簡化tableView代碼就顯得尤為重要。

簡化tableView在控制器VC中的代碼

而通常我們最直接的簡化代碼操作是繼承父類,把公共代碼放在父類控制器中,這是最簡單也是最有效的。還有一種是寫一個(gè)分類,在分類中寫公共方法,這在寫tableView是將會(huì)用到。

通常在寫VC代碼時(shí),自定義各種cell,避免不了重復(fù)無休止的寫一些沒有任何營養(yǎng)的代碼 如

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DiscoveryMenuContentView *cell = [tableView dequeueReusableCellWithIdentifier:[DiscoveryMenuContentView identifier] forIndexPath:indexPath];
            WS(weakSelf)
    cell.infoModelList = self.disoveryModel.infoList;
    return cell;
}

這將浪費(fèi)了大量的時(shí)間去寫相似代碼,而且代碼重用率非常低,耦合性太高。所以我們這里需要將能抽取出來的代碼全部都抽取出來。

只需要寫

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   return [tableView dequeueReusableCellAndLoadDataWithAdapter:self.dataArray[indexPath.row] delegate:self indexPath:indexPath];
}

就能夠使用于任何場景,而可以將這段代碼寫到父類中 所有的子類控制器只需要繼承或者重寫自定義即可。

而其實(shí)這段代碼只是寫在了tableView的分類中

@implementation UITableView (CustomCell)

- (CustomCell *)dequeueReusableCellAndLoadDataWithAdapter:(CellDataAdapter *)adapter indexPath:(NSIndexPath *)indexPath {
   
   CustomCell *cell = [self dequeueReusableCellWithIdentifier:adapter.cellReuseIdentifier forIndexPath:indexPath];
   [cell loadContentWithAdapter:adapter delegate:nil tableView:self indexPath:indexPath];
   
   return cell;
}

- (CustomCell *)dequeueReusableCellAndLoadDataWithAdapter:(CellDataAdapter *)adapter
                                                delegate:(id <CustomCellDelegate>)delegate
                                               indexPath:(NSIndexPath *)indexPath {
   
   CustomCell *cell = [self dequeueReusableCellWithIdentifier:adapter.cellReuseIdentifier forIndexPath:indexPath];
   [cell loadContentWithAdapter:adapter delegate:delegate tableView:self indexPath:indexPath];
   
   return cell;
}

抽取出來的只是dequeue的代碼及數(shù)據(jù)的一些賦值代碼,但是卻大大提高了效率


賦值操作則是寫在了adapter文件中(即分類中的CellDataAdapter),adapter文件之抽取出來的是一個(gè)抽象類,當(dāng)中有cell的賦值調(diào)用,cell數(shù)據(jù)源及cell高度等緩存一些操作。
而adapter是封裝了數(shù)據(jù)model的一個(gè)類,可以像添加model一樣添加到數(shù)據(jù)源數(shù)組中

+ (CellDataAdapter *)cellDataAdapterWithCellReuseIdentifier:(NSString *)cellReuseIdentifiers data:(id)data
                                                cellHeight:(CGFloat)cellHeight cellType:(NSInteger)cellType {
   
   CellDataAdapter *adapter    = [[self class] new];
   adapter.cellReuseIdentifier = cellReuseIdentifiers;
   adapter.data                = data;
   adapter.cellHeight          = cellHeight;
   adapter.cellType            = cellType;
   
   return adapter;
}

+ (CellDataAdapter *)cellDataAdapterWithCellReuseIdentifier:(NSString *)cellReuseIdentifiers data:(id)data
                                                cellHeight:(CGFloat)cellHeight cellWidth:(CGFloat)cellWidth
                                                  cellType:(NSInteger)cellType {
   
   CellDataAdapter *adapter    = [[self class] new];
   adapter.cellReuseIdentifier = cellReuseIdentifiers;
   adapter.data                = data;
   adapter.cellHeight          = cellHeight;
   adapter.cellWidth           = cellWidth;
   adapter.cellType            = cellType;
   
   return adapter;
}

我們?cè)谡{(diào)用的時(shí)候是可以直接賦值的

for (int i=0;i<result.count;i++) 
{
  [self.dataArray addObject:[CustomCell dataAdapterWithData:result[i]]];
}

在繼承自CustomCell的類中直接可以賦值操作,大大簡化了代碼,解耦。在中小型及多table項(xiàng)目中尤為方便。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,160評(píng)論 4 61
  • 愛之深就是當(dāng)你看見他和別的女孩在一起說說笑笑的時(shí)候你會(huì)很難過,、很想問他們?cè)谡f什么! 愛之深就是每次吃飯、或去...
    yeah米閱讀 103評(píng)論 0 0
  • 小時(shí)候,除了電視劇和書里,很少有人會(huì)真的說諸如溫柔、溫暖之類的詞,仿佛這種詞就像是一個(gè)幻影,鏡花水月,即便有時(shí)候呼...
    莊清嘉閱讀 721評(píng)論 0 0
  • 今天,與淘淘共讀《妖精的小孩》第二部。這一部分主要講述被妖精世界驅(qū)逐的薩思琪,在人類世界也受到排擠和非議,所...
    鏗鏘玫瑰llx閱讀 868評(píng)論 0 0
  • 現(xiàn)在是午夜12:50分,但是你還未歸家。 為了迎接工作上的新挑戰(zhàn),你已加班無數(shù)個(gè)夜晚。連最愛的足球也從之前一個(gè)禮拜...
    Marryanne閱讀 282評(píng)論 0 0

友情鏈接更多精彩內(nèi)容