第一种:不带indexPath
- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"MyCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
cell.textLabel.text = [NSString stringWithFormat:@"第%ld行",indexPath.row];
return cell;
}
第二种:带indexPath
- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath
//闲置池里单元格的标记
static NSString *identifier = @"Cell_01";
给单元格注册class
//给tableView的cell注册类型
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:identifier];
如果单元格是用nib文件定制的,就要用下面的方法注册nib文件
//给cell注册nib文件
[self.tableView registerNib:[UINib nibWithNibName:@"CustomTableViewCell" bundle:nil] forCellReuseIdentifier:identifier];
在TableView的代理方法中
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//根据indexPath从闲置池取cell,如果cell不存在,会根据注册的类型创建cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
cell.textLabel.text = [NSString stringWithFormat:@"section: %ld---cell %ld", indexPath.section, indexPath.row];
return cell;
}