【问题标题】:UITableview items becomes bold after scrolling upUITableview 项目向上滚动后变为粗体
【发布时间】:2014-01-31 04:49:03
【问题描述】:

好吧,也许我太挑剔了。如果您看不到它,请提前道歉,但在第二张图片中,该行上的项目有点像素化,或者好像一个词被放在了一个词的顶部。这仅在我向上滚动 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;

【问题讨论】:

  • 显示cellForRowAtIndexPath 方法的代码。
  • 不要直接给cell添加label,添加到cell的contentView中
  • 您的代码也会导致问题,同时重用单元格。重复使用单元格时,需要先移除现有标签,然后添加新标签。

标签: ios uitableview label uilabel tableview


【解决方案1】:

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

只需将以下代码放入 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;

这样,标签名称就不会一次又一次地添加到单元格中,因此不会发生此问题。

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

【讨论】:

    【解决方案2】:
    1. 不要直接给cell添加子视图,添加到cell的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;
    

    【讨论】:

    • 非常感谢您的帮助。我已经设法使用以下链接修复它,请让我知道我的方法的缺点是什么(因为它现在工作正常,它已经足够好了,不是吗?):oi57.tinypic.com/2rf79so.jpg
    • @user3255500:问题是您没有重复使用单元格。您每次都在创建单元格。因此,如果您有很多单元格,则会降低性能。第二个,您每次都在创建单元格,因此您不需要 dequeueReusableCellWithIdentifier: 调用,也不需要 if..else 条件(因为您在两种情况下都在做同样的事情)
    【解决方案3】:

    第 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];
        }
    

    【讨论】:

    • 嗨 bhavya,谢谢 4 的帮助。如果您可以评论我解决问题的解决方案的差异/消极性,而您的解决方案我将不胜感激。 oi57.tinypic.com/2rf79so.jpg
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-12
    • 2021-08-01
    相关资源
    最近更新 更多