UITableViewCell之注冊(cè)不注冊(cè)

結(jié)論:

所謂的注冊(cè)不注冊(cè)其實(shí)指:【有沒(méi)有為某一identifier 注冊(cè)一個(gè)Class】
或者理解為:有沒(méi)有把一個(gè)identifier和一個(gè)Class相互綁定。
如果發(fā)生綁定,當(dāng)標(biāo)識(shí)符為identifier 的Cell隊(duì)列中沒(méi)有可復(fù)用的cell時(shí),系統(tǒng)會(huì)自動(dòng)創(chuàng)建一個(gè)綁定的Class類型的cell。
如果沒(méi)有綁定Class,那么我們要手動(dòng)判定是否無(wú)可復(fù)用的cell,并手動(dòng)新建一個(gè)cell。
一句話總結(jié):注冊(cè)后就不用管cell是否需要新建了。

注冊(cè)情況:

當(dāng)使用register方法

//不使用nib
- (void)registerClass:(nullable Class)cellClass forCellReuseIdentifier:(NSString *)identifier;
//使用nib
- (void)registerNib:(nullable UINib *)nib forCellReuseIdentifier:(NSString *)identifier;

cellClass和identifier相互綁定,對(duì)應(yīng)使用dequeue方法(有indexPath參數(shù))

//使不使用nib對(duì)于dequeue無(wú)影響
- (__kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath;

這時(shí)候不需要判斷cell是否為空,判斷和創(chuàng)建步驟交由系統(tǒng)來(lái)自動(dòng)執(zhí)行。
注1:注冊(cè)的情況下cell默認(rèn)調(diào)用如下兩個(gè)方法實(shí)現(xiàn)實(shí)例化

//不使用nib
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier;
//使用nib
- (void)awakeFromNib;

系統(tǒng)默認(rèn)且僅會(huì)調(diào)用這兩個(gè)固定的方法來(lái)實(shí)現(xiàn)實(shí)例化。
所以若需要自定義,重寫(xiě)這兩個(gè)方法。
若需要傳參則需要單獨(dú)設(shè)立方法,在dequeue方法之后調(diào)用。
注2:一個(gè)class可以綁定多個(gè)identifier。

不注冊(cè)情況:

而不使用register方法,沒(méi)有把Class和identifier綁定,所以常見(jiàn)的寫(xiě)法是這樣的

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"identifier"];
if (cell == nil) {
     cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}

我們直接使用dequeue方法(無(wú)indexPath參數(shù)),然后手動(dòng)判斷是否取到可復(fù)用的cell。如果沒(méi)有取到,我們手動(dòng)的去實(shí)例化一個(gè)新的cell。
注:
不注冊(cè)的劣勢(shì)在于我們需要手動(dòng)判斷和手動(dòng)實(shí)現(xiàn)實(shí)例化
但優(yōu)勢(shì)在于,實(shí)例化的方法可以完全自定義,我們可以在實(shí)例化時(shí)就把參數(shù)傳入。

基礎(chǔ)示例:

1.基于class的注冊(cè),使用registerClass方法

//在viewDidLoad 中使用
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"identifier"];
//在tableView: cellForRowAtIndexPath: 中使用
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"identifier" forIndexPath:indexPath];

2.基于nib的注冊(cè),使用registerNib方法

//在viewDidLoad 中使用
[self.tableView registerNib:[UINib nibWithNibName:@"MyCommonCell" bundle:nil] forCellReuseIdentifier:@"identifier"];
//在tableView: cellForRowAtIndexPath: 中使用
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"identifier" forIndexPath:indexPath];

3.基于class的不注冊(cè),手動(dòng)判別cell是否為空

//在tableView: cellForRowAtIndexPath: 中使用
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"identifier"];
if (cell == nil) {
     cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}

4.基于class的不注冊(cè),手動(dòng)判別cell是否為空

//在tableView: cellForRowAtIndexPath: 中使用
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"identifier"];
if (cell == nil) {
     cell = [[[NSBundle mainBundle]loadNibNamed:@"MyCommonCell" owner:self options:nil] lastObject];
}

注:基于nib的創(chuàng)建也是不能夾帶參數(shù)的,實(shí)例化后單獨(dú)使用傳參方法。

?著作權(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),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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