开发者

UITableView Header Selection

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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜