cell 的注册重用与 cell 的创建方式息息相关,现整理了几种,希望对大家有帮助!
1. cell 通过 storyboard 、代码创建
1.1通过 storyboard 创建时,即在 storyboard 中创建 tableView,在 tableView 中直接添加 cell,添加几个 cell 代表有几种 cell,注册重用可以直接在 storyboard 中注册,直接上图!
1.2 通过代码创建,创建时后在 (以ViewController为例) ViewController.m 中注册,通过代码注册(上面通过 storyboard 创建的 cell 也可以通过这种方式 ,以代码注册,但是要注意,根据自测,在 storyboard 中注册过后,是不能通过代码修改 identifier 的)
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.tableView registerClass:[Cell class] forCellReuseIdentifier:@"cellZhuCe"];
}
2. 通过 xib 创建 cell 注册方法
2.1直接在 xib 中注册,直接上图
2.2 在代码中注册
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UINib * nib = [UINib nibWithNibName:NSStringFromClass([Cell class]) bundle:[NSBundle mainBundle]];
[self.tableView registerNib:nib forCellReuseIdentifier:@"cellZhuCe"];
}
两种重用方法分享给大家
1.
NSString * ID = @"Items";
GoodsCell * cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (!cell) {
cell = [[[NSBundle mainBundle] loadNibNamed:@"cellZhuCe" owner:nil options:nil]firstObject];
}
这种方法,以上四种注册皆可用2.
GoodsCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cellZhuCe" forIndexPath:indexPath];
这种方法代码简洁,但是在使用时应注意,在通过 xib 创建 cell 时,应该用代码注册(通过2.2的方法),直接在 xib 中注册,需要加载到ViewController里,但是怎么加载这么问题我没有解决,或者是别的原因?哪位朋友解决了,麻烦告知一声,万分感激!!!