結(jié)論:
所謂的注冊(cè)不注冊(cè)其實(shí)指:【有沒有為某一identifier 注冊(cè)一個(gè)Class】
或者理解為:有沒有把一個(gè)identifier和一個(gè)Class相互綁定。
如果發(fā)生綁定诸衔,當(dāng)標(biāo)識(shí)符為identifier 的Cell隊(duì)列中沒有可復(fù)用的cell時(shí),系統(tǒng)會(huì)自動(dòng)創(chuàng)建一個(gè)綁定的Class類型的cell颇玷。
如果沒有綁定Class,那么我們要手動(dòng)判定是否無可復(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無影響
- (__kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath;
這時(shí)候不需要判斷cell是否為空,判斷和創(chuàng)建步驟交由系統(tǒng)來自動(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è)固定的方法來實(shí)現(xiàn)實(shí)例化羞延。
所以若需要自定義,重寫這兩個(gè)方法脾还。
若需要傳參則需要單獨(dú)設(shè)立方法伴箩,在dequeue方法之后調(diào)用。
注2:一個(gè)class可以綁定多個(gè)identifier鄙漏。
不注冊(cè)情況:
而不使用register方法嗤谚,沒有把Class和identifier綁定,所以常見的寫法是這樣的
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"identifier"];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
我們直接使用dequeue方法(無indexPath參數(shù)),然后手動(dòng)判斷是否取到可復(fù)用的cell怔蚌。如果沒有取到巩步,我們手動(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ú)使用傳參方法。