iOS7:UILabel不断在UITableViewCell上重绘自己

时间:2014-05-06 17:39:00

标签: ios cocoa-touch uitableview

CoreData返回BOOL值,并根据值在UITableViewCell accessoryView上绘制UILabel。问题是UILabel也会在它不应该出现的细胞上重复。

CGRect lblRect = CGRectMake(230, 7, 20, 20);
    UILabel *lblEnabled = [[UILabel alloc] initWithFrame:lblRect];
    lblEnabled.textColor=[UIColor whiteColor];
    lblEnabled.textAlignment=NSTextAlignmentCenter;
    [lblEnabled setFont:[UIFont fontWithName:@"Verdana" size:10.0]];
    [lblEnabled setText:@"40"];
    lblEnabled.backgroundColor= [UIColor colorWithPatternImage:[UIImage imageNamed:@"greenBg"]];
    lblEnabled.layer.cornerRadius = 9.0;
    lblEnabled.layer.masksToBounds = YES;
    lblEnabled.tag = indexPath.row;
    cell.accessoryView = lblEnabled;
    [cell.contentView addSubview:lblEnabled];

所以它有时出现在BOOL value = NO的单元格上;非常感谢您的帮助。

编辑:我在cellForRowForIndexPath中绘制这些标签。

编辑:我使用故事板,所以我不检查单元格是否为零。

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

/*
if(cell==nil)
{
    cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    tableView.rowHeight=57.0;
}*/

Coin *coin=[[self frcFromTV:tableView ] objectAtIndexPath:indexPath];
cell.textLabel.text=coin.coinNominal;

if(coin.comSubject.length>0)
{
    cell.detailTextLabel.text=[NSString stringWithFormat:@"%@%@ (%@) | %@",[self returnCatalogDefinition:coin.catalogIndex],coin.kmRef, coin.dates, coin.comSubject];
}
else
{
    cell.detailTextLabel.text=[NSString stringWithFormat:@"%@%@ | %@",[self returnCatalogDefinition:coin.catalogIndex],coin.kmRef,coin.dates];
}

if(coin.isCommemorative.boolValue)
{
    // implement label
}

if(coin.listed.boolValue)
{
    CGRect lblRect = CGRectMake(230, 7, 20, 20);
    UILabel *lblEnabled = [[UILabel alloc] initWithFrame:lblRect];
    lblEnabled.textColor=[UIColor whiteColor];
    lblEnabled.textAlignment=NSTextAlignmentCenter;
    [lblEnabled setFont:[UIFont fontWithName:@"Verdana" size:10.0]];
    [lblEnabled setText:@"40"];
    lblEnabled.backgroundColor= [UIColor colorWithPatternImage:[UIImage imageNamed:@"greenBg"]];
    lblEnabled.layer.cornerRadius = 9.0;
    lblEnabled.layer.masksToBounds = YES;
    lblEnabled.tag = indexPath.row;
    cell.accessoryView = lblEnabled;
    [cell.contentView addSubview:lblEnabled];
}

return cell;
}

2 个答案:

答案 0 :(得分:1)

你在重复使用细胞吗?如果是这样,那么您需要从prepareForReuse方法中的contentView中删除该单元格。

答案 1 :(得分:1)

在下面的代码中,你可能会添加你的标签,但因为这些单元格是可重用的,你应该处理else语句(隐藏标签或任何合适的标签)

if(coin.isCommemorative.boolValue)
{
    // implement label
    //remove from that part of the statement this line:
    //[cell.contentView addSubview:lblEnabled];
} else {
     cell.accessoryView = nil;
    //hiding or modifying label for other cases
}

如果您不处理该其他声明,则由于重复使用机制,您所做的更改将适用于多个单元格

作为一个“侧面建议”,我建议你继承UITableViewCell的子类,并添加你想要的属性(标签)来封装它,并只使用公共方法来显示或隐藏该访问者。

编辑: 如果您的更改标志未指定它必须指示哪个单元格(例如使用indexPath),那么结果就是您的结果。

这是一个非常全局的状态if(coin.isCommemorative.boolValue),并未指示其计算的单元格(例如,仅用于学习目的)添加if(coin.isCommemorative.boolValue && indexPath.row%2==0)并查看结果。

相关问题