swift - UITableViewCell 中的 UISwitches 一起改变状态
问题描述
我的 Swift 项目中有一个非常奇怪的问题。我在我的一节中的动态单元格中使用 UIswitch。每当我点击第 6 个开关时,第一个开关就会随之改变其状态,反之亦然。该.valueChanged
功能仅适用于被点击的那个(正确的行为)。我无法弄清楚为什么开关会一起改变状态。
这是表格单元格的代码:
import UIKit
class RoutineTableViewCell: UITableViewCell {
@IBOutlet weak var selectionSwitch: UISwitch!
@IBOutlet weak var title: UILabel!
@IBOutlet weak var previewImage: UIImageView!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
这是我的表格视图控制器中的代码:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: RoutineTableReuseIdentifier, for: indexPath) as? RoutineTableViewCell
else{
return RoutineTableViewCell()
}
let exercise = section!.exercises[indexPath.row]
cell.title.text = exercise.title
cell.previewImage.image = UIImage(named: (exercise.gifName + ".gif"))
cell.selectionSwitch.addTarget(self, action: #selector(self.addRemoveExercise(_:)), for: .valueChanged)
return cell
}
@IBAction func addRemoveExercise(_ sender: UISwitch!) {
let buttonPosition:CGPoint = sender.convert(CGPoint.zero, to:self.tableView)
let indexPath = self.tableView.indexPathForRow(at: buttonPosition)
if(sender.isOn){
customizedSection?.exercises[indexPath!.row] = section!.exercises[indexPath!.row]
}
else{
customizedSection?.exercises[indexPath!.row] = ExerciseModel()
}
}
解决方案
你的表视图:
import UIKit
class TableViewController: UITableViewController,switchValues {
func changed(_ cell: UITableViewCell, _ mySwitch: UISwitch) {
let index = tableView.indexPath(for: cell)
switchStates[(index?.row)!] = mySwitch.isOn
tableView.reloadData()
}
var switchStates : [Bool] = [false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false]
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return switchStates.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MyTableViewCell
cell.mySwitch.isOn = switchStates[indexPath.row]
cell.delegate = self
return cell
}
你的手机:
import UIKit
protocol switchValues {
func changed(_ cell:UITableViewCell,_ mySwitch:UISwitch)
}
class MyTableViewCell: UITableViewCell {
@IBOutlet weak var mySwitch: UISwitch!
var delegate:switchValues!
@IBAction func valueChanged(_ sender: UISwitch) {
delegate.changed(self, mySwitch)
}
}
推荐阅读
- reactjs - 如何在 react 16.0.0 中迁移 componentWillReceiveProps?
- ios - 滚动 Swift 4 时,Collectionview 上的图像混合在一起
- javascript - 如何在 Node JS 中搜索和替换字符串
- scala - 将日志记录添加到设置键时设置不能依赖于任务
- javascript - 将 magnificPopup 添加到动态创建的元素
- javascript - 提交时,此表单不会运行验证脚本
- c++ - C++ 为什么编译器无法生成返回 Lambda 中捕获变量的 const 引用的代码
- php - PHP:getimagesize 为上传的文件返回 false
- javascript - 将 geoJSON 与另一个数组进行比较,如果存在匹配项,那么操作?
- ios - Navigation Controller & TableView: NavBar Hidden after hidden = true 一次