UITableView 标题选择
谁能解释一下如何检测表视图标题视图中的选择,例如 tableView:didSelectRowAtIndexPath: ?这对我来说非常有用。我已将标题定义为 UITableCellView,这样我就可以将 UISwitch 作为附件视图附加,它工作得很好,但现在我无法检测到开关中所做的更改。有什么建议吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设您是动态创建开关,而不是在 IB 中创建它们。如果是这种情况,您需要做两件事来接收和区分对开关的更改。首先,当您创建开关时,将其
tag
属性设置为表示相应部分的某个值(例如部分索引本身)。然后,将事件处理程序添加到开关,该事件处理程序将回调视图控制器上的方法。因此,在
tableView:viewForHeaderInSection:
中,延迟实例化您的标题视图,也许缓存它,然后说类似的话:并因此添加事件处理程序,其中 switchToggled: 的定义与任何其他 IBAction 一样:
然后,只需将您在事件处理程序中收到的
sender
强制转换为UISwitch *
,并使用tag
属性来判断哪个开关被切换。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:And add the event handler thusly, where switchToggled: is defined just as any other IBAction would be:
Then, just cast the
sender
you receive in your event handler toUISwitch *
, and use thetag
property to tell which switch was toggled.