Hasnain ahmad
Hasnain ahmad

Reputation: 301

How check one cell of tableView and uncheck rest of the cells in the section in swift

I am working on a project it's like a questionnaire in which I have added three cells in a section containing checkbox. what I want is when I check one checkbox of a cell it should uncheck the rest of the checkboxes of other cells in the same section. I am using checkbox library for check boxes

Upvotes: 0

Views: 565

Answers (1)

shynkhtb
shynkhtb

Reputation: 61

first of all you must create variable called selectedIndex in your vc

var selectedIndex: IndexPath!

after that in your tableviewcellforrow you must check selectedIndex

if self.selectedIndex == nil {
    cell.radioBtn.isselected = false
} else {
    if IndexPath == self.selectedIndex {
        cell.radioBtn.isslected = true
    } else {
        cell.radioBtn.isselected = false
    }
}

in your tableview cell after you check radio buttun you must update the selectedIndex

self.selectedIndex = indexPath

and after that

self.tableView.reloadData()

Upvotes: 1

Related Questions