参考:http://www.myexception.cn/gis/1867911.html
一.registerNib这种方式
1.首先xib中指定cell的Class为自定义cell类型(注意不是设置File's Owner的class)
2.调用 tableView 的 registerNib:forCellReuseIdentifier:方法向数据源注册cell
[self.tableView registerNib:[UINib nibWithNibName:@"ZYTableViewCell" bundle:nil] forCellReuseIdentifier:@"ZYTableViewCell"];
4、获取cell时若无可重用cell,将创建新的cell并调用其中的awakeFromNib方法,可通过重写这个方法添加更多页面内容
二:不使用xib(纯代码)
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
{
// cell页面布局
}
return self;
}
三.不使用xib, registerClass这种方式 (纯代码)
1.为tableView注册cell,使用registerClass:forCellReuseIdentifier:方法注册(注意是Class)
[self.tableView registerClass:[ZYTableViewCell class] forCellReuseIdentifier:@"ZYTableViewCell"];
ZYTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZYTableViewCell" forIndexPath:indexPath];