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.
There are two problems with your code:
In
newCell.accessoryView == cellViewyou compare the cell’s accessory view with a newlycreated 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 toUITableViewCellAccessoryCheckmarkagain. In other words, the secondelse ifblock will never be executed.The following code could work (but I did not try it myself):