在實(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)有了