UITableViewCell的重用声明有两种方式:
1. registerClass方式。在初始化UITableView的时候顺便声明一下cell的重用,如下所示。
[self.tableView registerClass:[CustomCell class] forCellReuseIdentifier:@"CustomCell"];
这时就不需要在(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法中添加如下代码:
static NSString *CellIdentifier = @"Cell";
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}//代码片段A
而只需要做如下声明:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
如果registerClass和代码片段A同时添加就会出现错误
2. cell == nil方式,该方式更常用,书写方式如代码片段A
static NSString *CellIdentifier = @"Cell";
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
参考文章:http://blog.csdn.net/u010367539/article/details/38900843