通过选择器传递参数以使选择器功能看到

通过选择器传递参数以使选择器功能看到

本文介绍了表视图单元的附件视图中的 UISwitch,通过选择器传递参数以使选择器功能看到 indexpath.row?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 tableviewcontroller 中有一个 UISwitch,当开关被切换时,我希望它改变我在视图控制器内创建的数组中的布尔变量的值,该单元格是相关的到.有点像 IOS 上的股票警报应用程序,其中每个单元格都有一个 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.row 值,而不将其作为参数传递给函数,因为我不能,因为它是一个选择器?

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
     }
}

这篇关于表视图单元的附件视图中的 UISwitch,通过选择器传递参数以使选择器功能看到 indexpath.row?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 05:58