ahmed
ahmed

Reputation: 105

Objective C Cell Accessory view

When I try to set the accessory view of a table view cell to a custom type image and run it in the simulator, I see that multiple cells have the accessory view image instead of the one that is currently selected. But when I set the accessory view to "cell.accessoryType = UITableViewCellAccessoryCheckmark;", I don't have this problem. Here is the code.

id item = [_items objectAtIndex:row];

if ([_currentVale isKindOfClass:[NSString class]])
{
    if ([item isEqualToString:_currentVale])
    {

        UIImageView *checkmark = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"checkmark-outline.png"]];
        checkmark.frame = CGRectMake(0, 0, 20, 20);
        cell.accessoryView = checkmark;
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
}
else if ([_currentVale isKindOfClass:[NSNumber class]])
{
    if ([item isEqualToNumber:_currentVale])
    {
        cell.accessoryType = UITableViewCellAccessoryNone;

    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;


    }
}

Two images of what I'm talking about. Only one value should be chosen in the picker but there is two showing the checkmark

Image 1

Image 2

Upvotes: 1

Views: 1416

Answers (2)

TomorJM
TomorJM

Reputation: 197

In the cell you don't want to show image,don't forget to set

cell.accessoryView = nil;

Upvotes: 0

Naveen Ramanathan
Naveen Ramanathan

Reputation: 2206

The cells are being reused. You need to set the accessory view also to nil inside the else.

else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.accessoryView = nil;
    }

Upvotes: 2

Related Questions