从 UIViewController 获取单元格中的 uiswitch 值 class
getting uiswitch value in cell from UIViewController class
我有一个带有 UITableView 的视图控制器。 Table 视图有 2 个单元格。其中一个单元格有一个 UI 开关。
在单元格 class 中,我声明了 UISwitch
cellSwitch.addTarget(self, action: #selector(switchChanged), for: UIControlEvents.valueChanged)
和一个函数
func switchChanged(mySwitch: UISwitch) {
let value = mySwitch.isOn
// Do something
print("mySwitch.isOn: \(value)")
}
我怎么知道我 changed.I 的哪个 UI 开关实例在运行时有多个形成
创建单元格时,将数据源索引添加为 UISwitch
标记。
在UISwitch
值变化时,获取控件的tag值。并使用通过控制标记获取的索引值从数据源数组中获取数据。
您应该在 UIViewController 中实现 UISwitch 值更改方法。否则你必须将数据源数组传递给每个单元格,这并不好。
创建单元格
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = // Your code here ....
// Implement UISwitch action method in the UIViewController only.
cell.cellSwitch.addTarget(self, action: #selector(switchChanged), for: UIControlEvents.valueChanged)
cell.cellSwitch.tag = indexPath.row
return cell
}
开关值已更改:
func switchChanged(mySwitch: UISwitch) {
let value = mySwitch.isOn
// Do something
print("mySwitch.isOn: \(value)")
// tag will give you the index of the data model.
let index = mySwitch.tag
//dataSourceArray is your tableview data source array. You can't get this array from cell. so better you should implement UISwitch value change method in the UIViewController.
let model = dataSourceArray[index];
// Now you got the model to update based on switch value change.
}
在您的自定义单元格 class 中,只需从故事板创建 UISwitch 按钮的插座和操作,如下所示
import UIKit
protocol CellDelegate: class {
func didTapCell(index: IndexPath)
}
class CustomeTableViewCell: UITableViewCell {
@IBOutlet weak var switchButton: UIButton!
var delegateCell:CellDelegate?
var indexPath:IndexPath?
@IBAction func yourSwitchButton(mySwitch: UISwitch) {
delegateCell?.didTapCell(index: indexPath!)
}
}
在你的ViewControllerClass
class ViewController: UIViewController,CellDelegate {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for:
indexPath) as! CustomCell
cell.delegateCell = self
cell.indexPath = indexPath
}
func didTapCell(index: IndexPath){
print("YOU SELECTED SWITCH \(index.row)")
}
}
我有一个带有 UITableView 的视图控制器。 Table 视图有 2 个单元格。其中一个单元格有一个 UI 开关。
在单元格 class 中,我声明了 UISwitch
cellSwitch.addTarget(self, action: #selector(switchChanged), for: UIControlEvents.valueChanged)
和一个函数
func switchChanged(mySwitch: UISwitch) {
let value = mySwitch.isOn
// Do something
print("mySwitch.isOn: \(value)")
}
我怎么知道我 changed.I 的哪个 UI 开关实例在运行时有多个形成
创建单元格时,将数据源索引添加为 UISwitch
标记。
在UISwitch
值变化时,获取控件的tag值。并使用通过控制标记获取的索引值从数据源数组中获取数据。
您应该在 UIViewController 中实现 UISwitch 值更改方法。否则你必须将数据源数组传递给每个单元格,这并不好。
创建单元格
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = // Your code here ....
// Implement UISwitch action method in the UIViewController only.
cell.cellSwitch.addTarget(self, action: #selector(switchChanged), for: UIControlEvents.valueChanged)
cell.cellSwitch.tag = indexPath.row
return cell
}
开关值已更改:
func switchChanged(mySwitch: UISwitch) {
let value = mySwitch.isOn
// Do something
print("mySwitch.isOn: \(value)")
// tag will give you the index of the data model.
let index = mySwitch.tag
//dataSourceArray is your tableview data source array. You can't get this array from cell. so better you should implement UISwitch value change method in the UIViewController.
let model = dataSourceArray[index];
// Now you got the model to update based on switch value change.
}
在您的自定义单元格 class 中,只需从故事板创建 UISwitch 按钮的插座和操作,如下所示
import UIKit
protocol CellDelegate: class {
func didTapCell(index: IndexPath)
}
class CustomeTableViewCell: UITableViewCell {
@IBOutlet weak var switchButton: UIButton!
var delegateCell:CellDelegate?
var indexPath:IndexPath?
@IBAction func yourSwitchButton(mySwitch: UISwitch) {
delegateCell?.didTapCell(index: indexPath!)
}
}
在你的ViewControllerClass
class ViewController: UIViewController,CellDelegate {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for:
indexPath) as! CustomCell
cell.delegateCell = self
cell.indexPath = indexPath
}
func didTapCell(index: IndexPath){
print("YOU SELECTED SWITCH \(index.row)")
}
}