Question

I've set a uiview with image in my cell's accessoryView and later on I want to remove this view so that the accessoryType can be shown once again as none. The following doesnt work -

  //create cell
        UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];

        //initialize double tick image
        UIImageView *dtick = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"dtick.png"]];
        [dtick setFrame:CGRectMake(0,0,20,20)];
        UIView * cellView = [[UIView alloc] initWithFrame:CGRectMake(0,0,20,20)];
        [cellView addSubview:dtick];

 //set accessory type/view of cell
        if (newCell.accessoryType == UITableViewCellAccessoryNone) {
            newCell.accessoryType = UITableViewCellAccessoryCheckmark;
            }
        else if(newCell.accessoryType == UITableViewCellAccessoryCheckmark){
                newCell.accessoryType = UITableViewCellAccessoryNone;
                newCell.accessoryView = cellView;
            }
        else if (newCell.accessoryView == cellView) {
            newCell.accessoryView = nil;
            newCell.accessoryType = UITableViewCellAccessoryNone;
          }

I've also tried [newCell.accessoryView reloadInputViews] but that doesn't work either.

Basically I want to cycle through these states upon click on cell => no tick -> one tick -> double tick (image) -> no tick

Any help is much appreciated, thanks.

Was it helpful?

Solution

There are two problems with your code:

  • In newCell.accessoryView == cellView you compare the cell's accessory view with a newly created image view: This comparison will never yield TRUE.

  • When you set the accessory view to your image, you also set the type to UITableViewCellAccessoryNone, so that the next time it will be set to UITableViewCellAccessoryCheckmark again. In other words, the second else if block will never be executed.

The following code could work (but I did not try it myself):

if (newCell.accessoryView != nil) {
     // image --> none
     newCell.accessoryView = nil;
     newCell.accessoryType = UITableViewCellAccessoryNone;
} else if (newCell.accessoryType == UITableViewCellAccessoryNone) {
     // none --> checkmark
     newCell.accessoryType = UITableViewCellAccessoryCheckmark;
} else if (newCell.accessoryType == UITableViewCellAccessoryCheckmark) {
     // checkmark --> image (the type is ignore as soon as a accessory view is set)
     newCell.accessoryView = cellView;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top