一、使用NIB
1、xib中指定cell的Class为自定义cell的类
2、调用registerNib:forCellReuseIdentifier:向数据源注册cell
[_tableView registerNib:[UINib nibWithNibName:@"xxx" bundle:nil] forCellReuseIdentifier:CellIdentify];
3、在tableView:cellForRowAtIndexPath:中使用dequeueReusableCellWithIdentifier:forIndexPath:获取重用的cell,如果没有重用的cell,将自动使用提供的nib文件创建cell并返回(如果使用dequeueReusableCellWithIdentifier:需要判断返回的是否为空)
xxx *cell= [_tableView dequeueReusableCellWithIdentifier:CellIdentify forIndexPath:indexPath];
4、获取cell时如果没有可重用cell,将创建新的cell并调用其中的awakeFromNib方法
二、不使用NIB
1、重写自定义cell的initWithStyle:withReuseableCellIdentifier:方法进行布局
2、注册
[_tableView registerClass:[CustomCell class] forCellReuseIdentifier:kCellIdentify];3、在tableView:cellForRowAtIndexPath:中使用dequeueReusableCellWithIdentifier:forIndexPath:获取重用的cell,如果没有重用的cell,将自动使用提供的class类创建cell并返回
xxx *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentify forIndexPath:indexPath];
4、获取cell时如果没有可重用的cell,将调用cell中的initWithStyle:withReuseableCellIdentifier:方法创建新的cell