列举两个场景对比一下,也许tableviewcell的复用就很清晰明了了。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UILabel *labelTest = [[UILabel alloc]init];
[labelTest setFrame:CGRectMake(2, 2, 80, 40)];
[labelTest setBackgroundColor:[UIColor clearColor]];
[labelTest setTag:1];
[[cell contentView]addSubview:labelTest];
}
UILabel *label1 = (UILabel*)[cell viewWithTag:1];
[label1 setText:[self.tests objectAtIndex:indexPath.row]];
return cell;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UILabel *labelTest = [[UILabel alloc]init];
[labelTest setFrame:CGRectMake(2, 2, 80, 40)];
[labelTest setBackgroundColor:[UIColor clearColor]]; //之所以这里背景设为透明,就是为了后面让大家看到cell上叠加的label。
[labelTest setTag:1];
[[cell contentView]addSubview:labelTest];
[labelTest setText:[self.tests objectAtIndex:indexPath.row]];
(转)UITableViewCell复用问题,布布扣,bubuko.com
原文:http://www.cnblogs.com/fanyufan/p/3587436.html