uitableviewcell 的附件视图中的 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 上的股票警报应用程序,其中每个单元格都有一个 UISwitch,切换开关将关闭每个单独的警报。因此,对于 UISwitch,及其选择器代码,这是在 cellForRowAtIndexPath 方法

//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 值,而不将其作为参数传递给函数,因为我不能因为它是一个选择器?

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

让我们将您的布尔值保存在数组中

    func switchTriggered(sender: UISwitch) {
      sender.on ? array[sender.tag]=1 : array[sender.tag]=0
     }
}

基本思路是,您可以捕获翻转开关的单元格,然后使用 tableView.indexPath(for:)UITableViewCell 引用转换为 NSIndexPath,然后您可以使用它 row 用于确定模型结构中的哪一行需要更新。

它的构成要素包括:

  1. 创建一个模型对象来捕获要在 table 视图中显示的信息。例如,假设每个单元格包含一个 Roomname 和一个反映灯是否亮着的布尔值:

    struct Room {
        var name: String
        var lightsOn: Bool
    }
    
  2. 然后 table 视图控制器会有一个数组:

    var rooms: [Room]!
    
  3. 我会定义一个 UITableViewCell subclass,带有用于标签和开关的插座。我还将电灯开关的 "value changed" 连接到该单元格中的方法。我还为单元格设置了一个协议,以通知其 table 视图控制器电灯开关已翻转:

    protocol RoomLightDelegate: class {
        func didFlipSwitch(for cell: UITableViewCell, value: Bool)
    }
    
    class RoomCell: UITableViewCell {
    
        weak var delegate: RoomLightDelegate?
    
        @IBOutlet weak var roomNameLabel: UILabel!
        @IBOutlet weak var lightSwitch: UISwitch!
    
        @IBAction func didChangeValue(_ sender: UISwitch) {
            delegate?.didFlipSwitch(for: self, value: sender.isOn)
        }
    
    }
    

    我显然将单元格原型的基础 class 设置为这个 UITableViewCell subclass 并连接 @IBOutlet 引用以及 @IBAction 用于更改开关的值。

  4. 然后我会让 UITableViewDataSource 方法根据 Room 属性填充单元格:

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return rooms.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "SwitchCell", for: indexPath) as! RoomCell
    
        let room = rooms[indexPath.row]
    
        cell.roomNameLabel.text = room.name
        cell.lightSwitch.setOn(room.lightsOn, animated: false)
        cell.delegate = self
    
        return cell
    }
    
  5. 注意,上面的 cellForRowAtIndexPath 也将自己指定为单元格的 delegate,所以我们要实现 RoomLightDelegate 协议来更新我们的翻转电灯开关时的模型:

    extension ViewController: RoomLightDelegate {
    
        func didFlipSwitch(for cell: UITableViewCell, value: Bool) {
            if let indexPath = tableView.indexPath(for: cell) {
                rooms[indexPath.row].lightsOn = value
            }
        }
    
    }
    

现在,我不想让你担心上面的细节。相反,尝试捕捉一些基本思想:

最重要的是,对于您的直接问题,一旦您知道更新了哪个单元格,您可以使用 UITableView 查询以确定 UITableViewCell 引用对应的 NSIndexPath,使用 tableView.indexPath(for:).

Swift 3 次更新:

let lightSwitch = UISwitch(frame: CGRect.zero) as UISwitch
lightSwitch.isOn = false
lightSwitch.addTarget(self, action: #selector(switchTriggered), for: .valueChanged)
lightSwitch.tag = indexPath.row
cell?.accessoryView = lightSwitch