Question

CoreData returns BOOL value and according to the value I draw a UILabel on UITableViewCell accessoryView. The problem is that UILabel repeats itself also on the cells it shouldn't appear at all.

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

So it appears sometimes on the cell where the BOOL value = NO; Your help is strongly appreciated.

EDIT: I draw these labels in cellForRowForIndexPath.

EDIT: I use storyboards, so I don't check if cell is nil.

-(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;
}
Was it helpful?

Solution

In code below you probably add your label but because those cells are reusable you should handle else statement (hiding label or whatever is appropriate)

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
}

if you will not deal with that else statement the change you made in if will be applicable to more than one cell because of the reusing mechanism

As a "side advice" I would recommend you to subclass UITableViewCell and add the property you want (label) to encapsulate that and make only public method for showing or hiding that accessor.

EDIT: if your flag for a change is not specifying to which cell it has to indicate (for example using indexPath) then the result is as your one.

This is quite global state if(coin.isCommemorative.boolValue) not indicating to which cell it counts try for example (for just learning purpose) add if(coin.isCommemorative.boolValue && indexPath.row%2==0) and see the result.

OTHER TIPS

Are you reusing cells? If so, then you need to remove that cell from contentView inside prepareForReuse method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top