2014-01-31 48 views
1

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

第一张图:

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

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

它更大胆出位和不登大雅之堂,一旦我向上滚动。

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

这是第二张图片的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; 
+0

显示'cellForRowAtIndexPath'方法的代码。 – rmaddy

+0

@rmaddy添加了它。 – user3255500

+0

不直接将标签添加到单元格,将其添加到单元格的内容视图 –

回答

2

发生这种情况,因为你不重用细胞,因此你基本上反复添加标签而不是使用已有的标签。

只要把下面的代码,如果(细胞==零)内:

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条件之外添加该元素的值。

+0

我可以知道这是什么投票? – Aish

1
  1. 不要添加子视图直接细胞,将其添加到内容查看电池
  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; 
+0

非常感谢你的帮助。我已经设法使用以下链接修复它,请让我知道我的方法的缺点是什么(因为它现在工作正常,它不够好吗?):http://oi57.tinypic.com/2rf79so.jpg – user3255500

+0

@ user3255500:问题是你没有重用单元。您每次都在创建单元格。所以如果你有很多单元,它会降低性能。第二个是你每次创建单元,所以你不需要这个'dequeueReusableCellWithIdentifier:'调用,并且不需要if..else条件(因为你在这两种情况下都做同样的事情) –

+0

明白了。说得通。谢谢您的帮助 ! – user3255500

1

两个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]; 
    } 
+0

嗨bhavya,thnx 4的帮助。如果你能评论我的解决方案的不同/消极性,解决这个问题,我会很感激你的。 http://oi57.tinypic.com/2rf79so.jpg – user3255500

相关问题