I need a FOR loop that will enable all other switch buttons to be turned off for the same cell when a switch button is turned on.
I try this but is not working if i have more than 4 cells.
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();")
}
}```
You shouldn't be trying to mix UI with logic. You should have a model that represents the rows in the table and each element of the model would have an isThisOneOn
property or something similar. When the user flips the switch in one of the cells, you enumerate all of the elements in the model and flip isThisOneOne
to false. Then you ask the table view to reload all of the cells what got flipped, using tableView.reloadRows(at: [indexPathsOfCellsThatGotFlipped, with: .automatic)