Ios 从UIViewController类获取单元格中的uiswitch值
我有一个带有UITableView的视图控制器。表视图有2个单元格。其中一个电池有一个开关 在cell类中,我声明UISwitchIos 从UIViewController类获取单元格中的uiswitch值,ios,swift3,xcode8,Ios,Swift3,Xcode8,我有一个带有UITableView的视图控制器。表视图有2个单元格。其中一个电池有一个开关 在cell类中,我声明UISwitch cellSwitch.addTarget(self, action: #selector(switchChanged), for: UIControlEvents.valueChanged) 和一个函数 func switchChanged(mySwitch: UISwitch) { let value = mySwitch.isOn //
cellSwitch.addTarget(self, action: #selector(switchChanged), for: UIControlEvents.valueChanged)
和一个函数
func switchChanged(mySwitch: UISwitch) {
let value = mySwitch.isOn
// Do something
print("mySwitch.isOn: \(value)")
}
我如何知道我更改了哪个UISwitch实例。我在运行时有几个实例正在形成。创建单元格时,将数据源索引添加为
UISwitch
标记
在ui开关
值更改时,获取控件的标记值。并使用通过控件标记获得的索引值从数据源数组中获取数据
您应该在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.
}
创建单元格时,将数据源索引添加为
UISwitch
标记
在ui开关
值更改时,获取控件的标记值。并使用通过控件标记获得的索引值从数据源数组中获取数据
您应该在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.
}
在自定义单元格类中,只需从故事板中创建UI开关按钮的出口和动作,如下所示
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!)
}
}
在您的ViewController类中
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)")
}
}
在自定义单元格类中,只需从故事板中创建UI开关按钮的出口和动作,如下所示
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!)
}
}
在您的ViewController类中
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)")
}
}
你的意思是在单个单元格中有多个UI开关?你的意思是在单个单元格中有多个UI开关?