Keith Kennedy
Keith Kennedy

Reputation: 320

UITableViewCell unselectable while its accessoryView is selectable

I want to have a UITableViewCell that is not selectable while its accessory view (a UISwitch in my case) is editable.

The issue is that I have two other cells of which one needs to remain active; this is very similar to the following image of the Time/Date selector from the iOS calendar app:

http://www.theiphoneblog.com/images/stories/2009/01/photo.jpg

I cannot post the image due to being a new user.

Note that in this view the "All-day" cell cannot be selected but its UISwitch can be changed, while one of "Starts" and "Ends" cells must remain selected.

I've tried both:

[cell setUserInteractionEnabled:NO];
[cell.contentView setUserInteractionEnabled:NO];

The first one works but does not allow the switch to be changed whereas the second does not work, it allows one of the top cells to be deselected which I do not want to happen.

Upvotes: 0

Views: 579

Answers (1)

Nekto
Nekto

Reputation: 17877

Implement in delegate method

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
     if ([selectablePaths contains:indexPath])
     {
          // cell selected
          selectedPath = indexPath;
     }else
     {
         [tableView deselectRowAtIndexPath:indexPath animated:NO];
         [tableView selectRowAtIndexPath:selectedPath animated:YES scrollPosition:UITableViewScrollPositionNone];
     }
 }

And enable user interaction for all cells.

Upvotes: 1

Related Questions