Search code examples
iosobjective-ciphoneobjective-c-2.0

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


Solution

  • 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;
        }