UISwitch在tableviewcell的附件视图中,传递一个参数与select器有select器函数看indexpath.row?

我在tableviewcontroller中有一个UISwitch ,当开关切换时,我希望它改变我在视图控制器内创build的数组中的布尔variables的值,该单元格与此有关。 有点像IOS上的股票报警应用,每个单元都有一个UISwitch ,切换开关将closures每个单独的报警。 所以对于UISwitch ,使用它的select器代码,这是在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值,而不是作为parameter passing给函数,因为我不能因为它的select器?

 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 

在Array中保存你的布尔值

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

基本的想法是,您可以捕获切换翻转的单元格,然后使用tableView.indexPath(for:)将该UITableViewCell引用转换为NSIndexPath ,并且可以使用它的row来标识模型结构中的哪一行需要要被更新。

这些构成要素包括:

  1. 创build一个模型对象来捕获要在表视图中显示的信息。 例如,让我们想象每个单元格都包含一个Roomname和一个反映灯是否点亮的布尔值:

     struct Room { var name: String var lightsOn: Bool } 
  2. 然后表视图控制器将有一个这样的数组:

     var rooms: [Room]! 
  3. 我会定义一个UITableViewCell子类与标签和交换机的sockets。 我还会将灯开关的“值改变”挂钩到该单元格中的方法。 我还要为单元设置一个协议,通知其表格视图控制器灯开关已翻转:

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

    我显然将单元格原型的基类设置为此UITableViewCell子类,并将@IBOutlet引用以及@IBOutlet连接起来,以便更改交换机的值。

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

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

底线,对于你的直接问题,一旦你知道哪个单元被更新了,你可以使用tableView.indexPath(for:)来查询UITableView来确定UITableViewCell引用对应的NSIndexPath

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