gpt4 book ai didi

ios - 表中的 UISwitch 在切换状态打开时选择多个单元格

转载 作者:行者123 更新时间:2023-11-28 08:00:20 25 4
gpt4 key购买 nike

我在我的 Swift 4 项目中创建了一个包含开关的自定义单元格。当我选择任何开关时,我会选择表中的每 10 个开关。表中有 34 个单元格。我尝试了许多启用/禁用用户交互的不同组合。即使未选定开关的状态显示为“打开”,相关操作也不会触发。这是触发操作的代码:

@IBAction func SwitchAction(_ sender: UISwitch ) {

let switchPosition = sender.convert(CGPoint(), to:tableView)
let indexPath = tableView.indexPathForRow(at:switchPosition)
let ipath = indexPath?.row
}

override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return taskList.count

}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 75
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "Cell")
let cell: customTableViewCell = self.tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! customTableViewCell
cell.LabelSelect.text=taskList[indexPath.row]

return cell
}

最佳答案

这是由于细胞被重复使用。当您滚动 tableView 时,一些行会离开屏幕;关联的单元格被放入队列中。其他行出现在屏幕上; cellForRowAt 方法被调用并从队列中取出一个单元格(因此 dequeueReusableCell),用于新行。如果当单元格离开屏幕时开关打开,那么当它出现在屏幕上时它仍然会打开 - 即使它现在与完全不同的行相关。

解决方案是维护一个数组,该数组指示给定行的开关是打开还是关闭。然后,您可以使用该数组在 cellForRowAt 方法中正确设置开关。

关于ios - 表中的 UISwitch 在切换状态打开时选择多个单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46841212/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com