滚动后,UITableview项目变为粗体

好的,也许我太挑剔了。 如果你看不到它,提前道歉,但在第二张图片中,行上的项目有点像素化,或者好像一个单词放在一个单词的顶部。 这只发生在我向上滚动tableview后,否则,它是类似于第一个图像。

第一张图:

http://oi61.tinypic.com/308grjc.jpg

第二张照片(你可以在这里看到字体的差异): http://oi61.tinypic.com/2mqpbb9.jpg

一旦我向上滚动,它会更加大胆和粗糙。

我坦率地不知道为什么。 tableview正在正常加载。 任何帮助,预感或build议都会有很大的关联,即使它可以指向正确的区域。

这里是第二个图像的tableview的代码。

static NSString *CellIdentifier = @"OrderProductCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease]; } //cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease]; //configure cell, display name and cost of each item OrderProduct *p = [[order.items allObjects] objectAtIndex:indexPath.row]; UILabel *lableName=[[UILabel alloc]initWithFrame:CGRectMake(10, 16, 300, 20)]; lableName.backgroundColor=[UIColor clearColor]; lableName.font=[UIFont boldSystemFontOfSize:19]; [lableName setFont:[UIFont fontWithName:@"Arial-Bold" size:12.0]]; lableName.text=p.name; //UILabel *counterNumber=[[UILabel alloc]initWithFrame:CGRectMake(10, 25, 300, 20)]; //counterNumber.backgroundColor=[UIColor clearColor]; //counterNumber.font=[UIFont systemFontOfSize:12]; //counterNumber.text=[NSString stringWithFormat:@"Scan time :%@",p.scannerCounter]; [cell.detailTextLabel setText:[NSString stringWithFormat:@"%@ %.02f",p.currency, p.cost]]; cell.imageView.image = [UIImage imageNamed:p.image]; [cell addSubview:lableName]; //[cell release]; //[cell addSubview:counterNumber]; return cell; 

发生这种情况是因为您不重复使用单元格,因此您基本上一次又一次地添加标签,而不是使用已有的标签。

只要把下面的代码放在if(cell == nil)里面:

 static NSString *CellIdentifier = @"OrderProductCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; UILabel *lableName; if(cell==nil){ // this is where we should initialize and customize the UI elements, // or in this case your label. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]; lableName=[[UILabel alloc]initWithFrame:CGRectMake(10, 16, 300, 20)]; lableName.backgroundColor=[UIColor clearColor]; lableName.font=[UIFont boldSystemFontOfSize:19]; [lableName setFont:[UIFont fontWithName:@"Arial-Bold" size:12.0]]; [cell addSubview:lableName]; } // this is where we should assign the value. OrderProduct *p = [[order.items allObjects] objectAtIndex:indexPath.row]; lableName.text = p.name; /* assign other values if any ... */ return cell; 

这样lableName将不会被一次又一次地添加到单元中,因此这个问题不会发生。

重用单元格属性的最好方法是初始化和定制if(cell == nil)中的所有内容,并且只在if条件之外添加元素的值。

  1. 不要直接将子视图添加到单元格,将其添加到单元格的contentView
  2. 在重新使用单元格时,您需要首先删除现有标签并添加新标签或使用现有单元格,而不是添加新标签。

改变你的代码,如:

 static NSString *CellIdentifier = @"OrderProductCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; UILabel *lableName = nil; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease]; lableName=[[UILabel alloc]initWithFrame:CGRectMake(10, 16, 300, 20)]; lableName.backgroundColor=[UIColor clearColor]; lableName.font=[UIFont boldSystemFontOfSize:19]; [lableName setFont:[UIFont fontWithName:@"Arial-Bold" size:12.0]]; lableName.tag = 7; [cell.contentView addSubview:lableName]; } //configure cell, display name and cost of each item OrderProduct *p = [[order.items allObjects] objectAtIndex:indexPath.row]; lableName = (UILabel *)[cell.contentView viewWithTag:7]; lableName.text=p.name; [cell.detailTextLabel setText:[NSString stringWithFormat:@"%@ %.02f",p.currency, p.cost]]; cell.imageView.image = [UIImage imageNamed:p.image]; return cell; 

第1行和第2行都做相同的设置字体

  lableName.font=[UIFont boldSystemFontOfSize:19]; // line 1 [lableName setFont:[UIFont fontWithName:@"Arial-Bold" size:12.0]]; // line 2 [cell.contentView addSubview:lableName]; UILabel *lableName = nil; //Inside your cellForRowAtIndexPath put this code static NSString *CellIdentifier = @"OrderProductCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease]; } //cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease]; //configure cell, display name and cost of each item OrderProduct *p = [[order.items allObjects] objectAtIndex:indexPath.row]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease]; lableName=[[UILabel alloc]initWithFrame:CGRectMake(10, 16, 300, 20)]; lableName.backgroundColor=[UIColor clearColor]; [lableName setFont:[UIFont fontWithName:@"Arial-Bold" size:12.0]]; // set fonts only once [cell.contentView addSubview:lableName]; }