iOS封裝倒脓、繼承、多態(tài)項目應(yīng)用-tableview(OC篇)

概念

封裝:提供可以調(diào)用的接口含思,隱藏具體實現(xiàn)方案
繼承:子類擁有父類的成員變量和方法崎弃,可以較大程度減少代碼的重復(fù)率
多態(tài):父類指針指向子類對象(實例對象調(diào)用的方法會尋找到真實的類進(jìn)行調(diào)用)

應(yīng)用

封裝一個常用的UITableView為例子

  • 普通的tableView復(fù)用代碼
    WCBaseTableViewCell
@implementation WCBaseTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self prepareUI];
    }
    return self;
}

- (void)prepareUI {
    self.backgroundColor = [UIColor colorWithRed:random() % 255 / 255.0  green:random() % 255 / 255.0 blue:random() % 255 / 255.0 alpha:1.0];
}

@end

ViewController

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ID = @"WCBaseTableViewCell";
    WCBaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[WCBaseTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    return cell;
}

效果

*效果*


  • UITableViewCell的初步封裝
    從上面的tableViewCell創(chuàng)建的過程來看(如下)
static NSString *ID = @"WCBaseTableViewCell";
    WCBaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[WCBaseTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    return cell;

既然每次實例化一個cell對象都需要寫這么一大段代碼甘晤,那么是否可以把這些代碼復(fù)用起來(如果WCBaseTableViewCell在多個控制器中使用到,那么就會使得控制器會有很多重復(fù)的代碼)饲做,于是线婚,我們想到了類方法來進(jìn)行對象的實例化,方法如下:
WCBaseTableViewCell.h

@interface WCBaseTableViewCell : UITableViewCell

+ (instancetype)wc_baseTableViewCellWithTableView:(UITableView *)tableView;

@end

WCBaseTableViewCell.m

@implementation WCBaseTableViewCell

+ (instancetype)wc_baseTableViewCellWithTableView:(UITableView *)tableView {
    static NSString *ID = @"WCBaseTableViewCell";
    WCBaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[WCBaseTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self prepareUI];
    }
    return self;
}

- (void)prepareUI {
    self.backgroundColor = [UIColor colorWithRed:random() % 255 / 255.0  green:random() % 255 / 255.0 blue:random() % 255 / 255.0 alpha:1.0];
}

@end
  • ViewController中的使用*
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WCBaseTableViewCell *cell = [WCBaseTableViewCell wc_baseTableViewCellWithTableView:tableView];
    return cell;
}

總結(jié)
以上的普通的tableView復(fù)用代碼UITableViewCell的初步封裝結(jié)果是一致的盆均,使用了工廠方法塞弊,將一部分在控制器中常使用的代碼放到了cell里面,使得控制器的代碼更加易讀和簡潔泪姨,而WCBaseTableViewCell類中的這一部分代碼游沿,提供了接口供外部使用聲明變量,這驴娃,就是封裝奏候。


  • UITableViewCell的二次封裝(進(jìn)階-多態(tài)和繼承)
    從以上的方法來看,是將原本應(yīng)該在VC中的cell的實例化的代碼封裝到了cell里作為類方法來使用唇敞,這解決了以下問題:在不同地方用到相同的cell的時候需要寫一大段的代碼進(jìn)行變量的聲明蔗草。但是!這同樣會有重復(fù)代碼的問題疆柔,比如咒精,不同的cell,一樣會需要寫一大段類似的類方法進(jìn)行聲明旷档,如下
    WCBaseTableViewCell
@implementation WCFirstTableViewCell

+ (instancetype)wc_firstTableViewCellWithTableView:(UITableView *)tableView {
    static NSString *ID = @"WCFirstTableViewCell";
    WCFirstTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[WCFirstTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self prepareUI];
    }
    return self;
}

- (void)prepareUI {
    
}

@end
@implementation WCSecondTableViewCell

+ (instancetype)wc_secondTableViewCellWithTableView:(UITableView *)tableView {
    static NSString *ID = @"WCSecondTableViewCell";
    WCSecondTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[WCSecondTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self prepareUI];
    }
    return self;
}

- (void)prepareUI {
    
}

@end

這是不是又碰到了和上面一樣的問題-類似的代碼需要重復(fù)的寫模叙,那么,有沒有方法可以避免問題的產(chǎn)生呢鞋屈?范咨!當(dāng)然有,這個時候厂庇,我們的繼承渠啊,就發(fā)揮出了獨特的作用。我們可以設(shè)計一個基類(基類擁有一個公共的類方法)权旷,讓其他的子類繼承這個基類替蛉,這樣就可以避免重復(fù)寫類似的代碼。那么拄氯,唯一要解決的兩個:

  1. 基類類方法里的cell標(biāo)識符如何根據(jù)不同的子類聲明不同
  2. 基類類方法的init類如何顯示為字類
    以上兩個問題剛好可以用多態(tài)來解決躲查!
    WCBaseTableViewCell.m
@implementation WCBaseTableViewCell

+ (instancetype)wc_baseTableViewCellWithTableView:(UITableView *)tableView {
    NSString *ID = NSStringFromClass(self.class);
    WCBaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[self.class alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self prepareUI];
    }
    return self;
}

- (void)prepareUI {
}

@end

WCBaseTableViewCell.h

@interface WCBaseTableViewCell : UITableViewCell

+ (instancetype)wc_baseTableViewCellWithTableView:(UITableView *)tableView;
- (void)prepareUI;

@end

這樣,一個基類就完成了译柏,實戰(zhàn)如下
聲明一個繼承這個基類WCBaseTableViewCell的類
WCThirdTableViewCell.h

#import "WCBaseTableViewCell.h"

NS_ASSUME_NONNULL_BEGIN

@interface WCThirdTableViewCell : WCBaseTableViewCell

@end

NS_ASSUME_NONNULL_END

WCThirdTableViewCell.m

@implementation WCThirdTableViewCell

- (void)prepareUI {
    [super prepareUI];
    self.backgroundColor = [UIColor colorWithRed:random() % 255 / 255.0 green:random() % 255 / 255.0 blue:random() % 255 / 255.0 alpha:1.0];
}

@end

在VC中調(diào)用如下
ViewController

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WCThirdTableViewCell *cell = [WCThirdTableViewCell wc_baseTableViewCellWithTableView:tableView];
    return cell;
}

效果如下


效果圖

后續(xù)如有要的新的cell镣煮,只需要繼承WCBaseTableViewCell,然后重寫prepareUI方法即可鄙麦。


此方式的swift版本在后續(xù)更新

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末怎静,一起剝皮案震驚了整個濱河市邮弹,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌蚓聘,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,525評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件盟劫,死亡現(xiàn)場離奇詭異夜牡,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)侣签,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,203評論 3 395
  • 文/潘曉璐 我一進(jìn)店門塘装,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人影所,你說我怎么就攤上這事蹦肴。” “怎么了猴娩?”我有些...
    開封第一講書人閱讀 164,862評論 0 354
  • 文/不壞的土叔 我叫張陵阴幌,是天一觀的道長。 經(jīng)常有香客問我卷中,道長矛双,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,728評論 1 294
  • 正文 為了忘掉前任蟆豫,我火速辦了婚禮议忽,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘十减。我一直安慰自己栈幸,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,743評論 6 392
  • 文/花漫 我一把揭開白布帮辟。 她就那樣靜靜地躺著速址,像睡著了一般。 火紅的嫁衣襯著肌膚如雪织阅。 梳的紋絲不亂的頭發(fā)上壳繁,一...
    開封第一講書人閱讀 51,590評論 1 305
  • 那天,我揣著相機(jī)與錄音荔棉,去河邊找鬼闹炉。 笑死,一個胖子當(dāng)著我的面吹牛润樱,可吹牛的內(nèi)容都是我干的渣触。 我是一名探鬼主播,決...
    沈念sama閱讀 40,330評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼壹若,長吁一口氣:“原來是場噩夢啊……” “哼嗅钻!你這毒婦竟也來了皂冰?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,244評論 0 276
  • 序言:老撾萬榮一對情侶失蹤养篓,失蹤者是張志新(化名)和其女友劉穎秃流,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體柳弄,經(jīng)...
    沈念sama閱讀 45,693評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡舶胀,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,885評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了碧注。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片嚣伐。...
    茶點故事閱讀 40,001評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖萍丐,靈堂內(nèi)的尸體忽然破棺而出轩端,到底是詐尸還是另有隱情,我是刑警寧澤逝变,帶...
    沈念sama閱讀 35,723評論 5 346
  • 正文 年R本政府宣布基茵,位于F島的核電站,受9級特大地震影響骨田,放射性物質(zhì)發(fā)生泄漏耿导。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,343評論 3 330
  • 文/蒙蒙 一态贤、第九天 我趴在偏房一處隱蔽的房頂上張望舱呻。 院中可真熱鬧,春花似錦悠汽、人聲如沸箱吕。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,919評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽茬高。三九已至,卻和暖如春假抄,著一層夾襖步出監(jiān)牢的瞬間怎栽,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,042評論 1 270
  • 我被黑心中介騙來泰國打工宿饱, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留熏瞄,地道東北人。 一個月前我還...
    沈念sama閱讀 48,191評論 3 370
  • 正文 我出身青樓谬以,卻偏偏與公主長得像强饮,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子为黎,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,955評論 2 355

推薦閱讀更多精彩內(nèi)容