我需要一个 FOR 循环,以便在打开开关按钮时关闭同一单元格的所有其他开关按钮。
我尝试了这个,但如果我有超过 4 个电池,则不起作用。
func switchButtonStateChanged(isOn: Bool, indexPath: IndexPath) {
if pravnoLice == 0 && baby == false {
cellRow = indexPath[0] > 0 ? indexPath[1] + tableView.numberOfRows(inSection: 0) : indexPath[1]
} else {
cellRow = indexPath[0] > 1 ? indexPath[1] + tableView.numberOfRows(inSection: 1) : indexPath[1]
}
if isOn {
webView.evaluateJavaScript("document.getElementById('Insurees_\(cellRow)__IsPolicyHolder').click();")
// Switch logika
for i in 0..<tableView.numberOfRows(inSection: indexPath.section) {
if i != indexPath.row{
if let otherCell = tableView.cellForRow(at: IndexPath(row: i, section: indexPath.section)) as? InfoTableViewCell {
otherCell.policyHolderSwitch.isOn = false
}
}
}
} else {
webView.evaluateJavaScript("document.getElementById('Insurees_\(cellRow)__IsPolicyHolder').click();")
}
}```
您不应该尝试将 UI 与逻辑混合在一起。您应该有一个代表表中行的模型,并且模型的每个元素都有一个
isThisOneOn
属性或类似的属性。当用户翻转其中一个单元格中的开关时,您将枚举模型中的所有元素并将 isThisOneOne
翻转为 false。然后,您使用 tableView.reloadRows(at: [indexPathsOfCellsThatGotFlipped, with: .automatic)
要求表格视图重新加载所有翻转的单元格