自定义单元格中的标签重叠

问题描述:

我已经使用四个标签自定义了单元格,最初它看起来很好但是当我滚动然后从另一行重叠选择单元格标签时。这是我的代码。任何帮助都将受到高度赞赏。

I have customized the cell with four labels, initially it looks fine but when I scroll and then select a cell labels from another row overlaps. here is my code. Any help will be highly appreciated.

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier=@"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];

if (cell == nil) { cell = [[[UITableViewCell alloc]
                            initWithStyle:UITableViewCellStyleDefault 
                            reuseIdentifier: CellIdentifier] autorelease];
}
 NSString *cellIconName = [[self cellIcon] objectAtIndex:[indexPath row]];
cellImage=[UIImage imageNamed:cellIconName];

cell.imageView.image=cellImage;

UILabel *labelOne = [[UILabel alloc]initWithFrame:CGRectMake(65, 10, 140, 20)];
labelOne.text = [[NSString alloc]initWithFormat:@"%@",[dataList objectAtIndex:indexPath.row]];
[cell.contentView addSubview:labelOne];
[labelOne release];

UILabel *labelTwo = [[UILabel alloc]initWithFrame:CGRectMake(260, 10, 140, 20)];
labelTwo.text = [[NSString alloc]initWithFormat:@"%@",[newPrice objectAtIndex:indexPath.row]];
[cell.contentView addSubview:labelTwo];
[labelTwo release];



UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(65, 30, 140, 20)];
label3.text=[[NSString alloc]initWithFormat:@"%@",[details objectAtIndex:indexPath.row]];
[cell.contentView addSubview:label3];
[label3 release];


UILabel *label4 = [[UILabel alloc]initWithFrame:CGRectMake(260, 30, 140, 20)];
label4.text=[[NSString alloc]initWithFormat:@"%@",[oldPrice objectAtIndex:indexPath.row]];
[cell.contentView addSubview:label4];
[label4 release];

return cell;

}

错误是您在if条件 if(cell == nil)之外的(else部分)中分配了标签。首次创建单元格时,您应该只创建一次标签。

The mistake is you allocated the labels in (else part)outside of the if condition if(cell==nil). You should only create the labels once, when the cell is first created.

当我们运行代码时,控件会检查单元格是否为nil或具有任何值。如果我们每次都在else部分分配标签(同时滚动),那么每次都会创建一个新标签。如果我们在if条件中分配它,标签只为每个单元格分配一次,我们只是在滚动时更新文本值。

When we run the code the control checks whether the cell is nil or have any values. If we allocate the label in else part every time (while scrolling also) you are creating a new label every time. If we allocate it in if condition, the label is only allocated once for each cell and we just update the text value when scrolling.

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier=@"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];

if (cell == nil) 
{ 
cell = [[[UITableViewCell alloc]
                            initWithStyle:UITableViewCellStyleDefault 
                            reuseIdentifier: CellIdentifier] autorelease];

NSString *cellIconName = [[self cellIcon] objectAtIndex:[indexPath row]];
cellImage=[UIImage imageNamed:cellIconName];

cell.imageView.image=cellImage;

UILabel *labelOne = [[UILabel alloc]initWithFrame:CGRectMake(65, 10, 140, 20)];
labelOne.tag = 100;
[cell.contentView addSubview:labelOne];
[labelOne release];

UILabel *labelTwo = [[UILabel alloc]initWithFrame:CGRectMake(260, 10, 140, 20)];
labelTwo.tag = 101;
[cell.contentView addSubview:labelTwo];
[labelTwo release];

}

UILabel *labelOne = (UILabel *) [cell.contentView viewWithTag:100];
labelOne.text = [dataList objectAtIndex:indexPath.row];

UILabel *labelTwo = (UILabel *) [cell.contentView viewWithTag:101];
labelTwo.text = [newPrice objectAtIndex:indexPath.row];
 

return cell;
}