在tableviewcell的附件视图中的UISwitch,使用选择器传递参数以使选择器函数看到indexpath.row? [英] UISwitch in accessory view of a tableviewcell, passing a parameter with the selector to have selector function see the indexpath.row?
问题描述
我在tableviewcontroller中有一个 UISwitch
,当切换开关时,我希望它改变我在视图控制器中创建的数组中的布尔变量的值,细胞是与之相关的。有点像IOS上的Stock Alarm App,其中每个单元格都有一个 UISwitch
,并且切换开关将关闭每个单独的警报。所以使用 UISwitch
及其选择器代码,这是在 cellForRowAtIndexPath
方法
I have a UISwitch
in a tableviewcontroller, and when the switch is toggled I want it to change the value of a boolean variable in an array I created inside the view controller, that the cell is related to. Kind of like the Stock Alarm App on IOS, where each cell has a UISwitch
, and toggling the switch will turn off each individual alarm. So with the UISwitch
, with its selector code, this is inside the cellForRowAtIndexPath
method
//switch
let lightSwitch = UISwitch(frame: CGRectZero) as UISwitch
lightSwitch.on = false
lightSwitch.addTarget(self, action: #selector(switchTriggered), forControlEvents: .ValueChanged)
//lightSwitch.addTarget(self, action: "switchTriggered", forControlEvents: .ValueChanged )
cell.accessoryView = lightSwitch
我希望它能做到这一点
func switchTriggered(a: Int) {
changeValueOfArray = array[indexPath.row]
}
我还没有为该部分编写代码,但我的问题是,如何让switchTriggered函数看到 indexPath。行
值,不将它作为参数传递给函数,因为我不能因为它的选择器?
I don't have the code written for that part yet, but my question is, How can i let the switchTriggered function see the indexPath.row
value, without passing it as an argument to the function because I can't because its a selector?
推荐答案
let lightSwitch = UISwitch(frame: CGRectZero) as UISwitch
lightSwitch.on = false
lightSwitch.addTarget(self, action: #selector(switchTriggered), forControlEvents: .ValueChanged)
lightSwitch.tag = indexpath.row
cell.accessoryView = lightSwitch
让你的布尔值保存在数组中
Let save your boolean value in Array
func switchTriggered(sender: UISwitch) {
sender.on ? array[sender.tag]=1 : array[sender.tag]=0
}
}
这篇关于在tableviewcell的附件视图中的UISwitch,使用选择器传递参数以使选择器函数看到indexpath.row?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!