UITableViewCell的四種復(fù)用的方法

在實(shí)際的開發(fā)過程中,我們總要去復(fù)用cell,最好自定義cell,然后注冊(cè),那么今天來總結(jié)一下cell復(fù)用的四種方法,兩個(gè)是純代碼,兩個(gè)是用nib復(fù)用

圖1.所有的效果都是這樣的,經(jīng)過測(cè)試,都是有復(fù)用
//第一中cell的復(fù)用標(biāo)識(shí)
static  NSString* firseCellid = @"firseCell";
//第2中cell的復(fù)用標(biāo)識(shí)
static  NSString* secCellid = @"secCellid";
//第3中cell的復(fù)用標(biāo)識(shí)
static  NSString* threeCellid = @"threeCellid";
//第4中cell的復(fù)用標(biāo)識(shí)
static  NSString* fourCellid = @"fourCellid";

方法一,使用純代碼,在控制器中使用registerClass注冊(cè)cell

//1.使用tableview注冊(cè)cell,“registerClass”專門注冊(cè)純代碼cell的
- (void)viewDidLoad {
    [super viewDidLoad];
    //注冊(cè)firseCell
    [self.tableView registerClass:[SEFirstCell class] forCellReuseIdentifier:firseCellid];
}


//2.在數(shù)據(jù)源方法中,直接“dequeueReusableCellWithIdentifier”,獲取到復(fù)用的cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SEFirstCell *cell = [tableView dequeueReusableCellWithIdentifier:firseCellid];
    cell.textLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row];
    NSLog(@"%@",cell);
    return cell;
}

方法2,使用純代碼,在cell中調(diào)用方法initWithStyle: reuseIdentifier:];設(shè)置重用標(biāo)識(shí)符(推薦)

//1.SESecCell.h文件中,添加一個(gè)類方法
+ (instancetype)secCellWithTableView:(UITableView *)tableView;
//2.SESecCell.m文件中,開始處理這個(gè)類方法,在cell中調(diào)用方法`initWithStyle: reuseIdentifier:];`設(shè)置重用標(biāo)識(shí)符
+ (instancetype)secCellWithTableView:(UITableView *)tableView{
    SESecCell *cell = [tableView dequeueReusableCellWithIdentifier:secCellid];
    if (!cell) {
        cell = [[SESecCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:secCellid];
    }
    return cell;
}
//在控制器中,調(diào)用cell的時(shí)候,直接使用類方法就好,非常的方便
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SESecCell *cell = [SESecCell secCellWithTableView:tableView];
    cell.textLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row];
    return cell;
}

方法3,使用xib,在控制器中使用registerNib注冊(cè)cell

圖2.先寫一個(gè)nib,設(shè)置一下重用標(biāo)識(shí)
//1.使用tableview注冊(cè)nib,記得使用重用標(biāo)識(shí)
- (void)viewDidLoad {
    [super viewDidLoad];
    //注冊(cè)threeCell
    [self.tableView registerNib:[UINib nibWithNibName:@"SEThreeCell" bundle:nil] forCellReuseIdentifier:threeCellid];
}


//2.在數(shù)據(jù)源方法中,直接“dequeueReusableCellWithIdentifier”,獲取到復(fù)用的cell(和方法一相同)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SEThreeCell *cell = [tableView dequeueReusableCellWithIdentifier:threeCellid];
    cell.textLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row];
        NSLog(@"%@",cell);
    return cell;
}

方法4,使用nib,在cell方法中直接注冊(cè)nib (推薦)

//1.先寫一個(gè)類方法
#import <UIKit/UIKit.h>
@interface SEFourCell : UITableViewCell
+ (instancetype)fourCellWithTableView:(UITableView *)tableView;
@end
//2.將這個(gè)方法重寫一下,加載和注冊(cè)nib
+ (instancetype)fourCellWithTableView:(UITableView *)tableView{
    UINib *nib = [UINib nibWithNibName:@"SEFourCell" bundle:nil];
    [tableView registerNib:nib forCellReuseIdentifier:fourCellid];
    SEFourCell *fourCell = [[nib instantiateWithOwner: nib options:nil] lastObject];
    return fourCell;
}
//3.直接使用,已經(jīng)復(fù)用了~
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        SEFourCell *cell = [SEFourCell fourCellWithTableView:tableView];
        cell.textLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row];
        NSLog(@"%@",cell);
        return cell;
}

剛才我測(cè)試了一下,就是使用nib的時(shí)候,圖2中設(shè)置了identifier的標(biāo)記,這個(gè)寫不寫都行,因?yàn)樵诖a中已經(jī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),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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