views:

137

answers:

1

Hello,

Can anyone explain me how can I detect a selection in a table view Header view like in tableView:didSelectRowAtIndexPath: ? It would be very useful for me. I've defined the header as UITableCellView so I could attach a UISwitch as an accessory view, and it worked great but now I can't detect the changes made in the switch. Any suggestions?

A: 

I'm going to assume you're dynamically creating the switches, not creating them in IB. If that's the case, you'll need to do two things to receive and distinguish changes to your switches. First, when you create the switch, set its tag property to some value that represents the corresponding section (like maybe the section index itself). Then, add an event handler to the switch that will call back to a method on your view controller.

So, in tableView:viewForHeaderInSection:, lazily instantiate your header view, maybe caching it, then say something like:

[switch setTag:section];

And add the event handler thusly, where switchToggled: is defined just as any other IBAction would be:

[switch addTarget:self 
           action:@selector(switchToggled:)
 forControlEvents:UIControlEventValueChanged];

Then, just cast the sender you receive in your event handler to UISwitch *, and use the tag property to tell which switch was toggled.

warrenm