Checkbox Issue in tableview cell

Viewed 33

I have a multiple checkbox in tableview cell and total number of cell 4,i want to select one checkbox at one time and all check box is unselect if i select any one of check box and also one button in table view cell button are also disable of other cell so how i implement this logic.here i implement image check and uncheck logic

func setupData(plansModel: [Plans], mainRowIndex: Int, selctedPlan: Int, isAllUnselected: Bool){
    self.plansModel = plansModel
    indexMain = mainRowIndex
    selectedPlanId = selctedPlan
    self.isAllUnselected = isAllUnselected
    self.changePlanBtn.isHidden = isAllUnselected
    
    priceTableView.reloadData()
}

i take this variable for data and manage checkbox

1 Answers

I think you want this

First

second

You can do this Way! Firts make a variable

var selectedIndex: IndexPath = IndexPath(row: 0, section: 0)

Second in tableview didselecte method add this

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let previousSelectedIndex = selectedIndex
    selectedIndex = indexPath
    checkBoxTableView.reloadRows(at: [previousSelectedIndex, selectedIndex], with: .none)
}

third and last add this code in tableview cellForRow Method

if selectedIndex.row == indexPath.row {
    cell.accessoryType = .checkmark
} else {
    cell.accessoryType = .none
}

I think your answer is solved.

This Was edited For realod only two row rather than whole tableview (this edit rafrence by paulw11).

Thank you Paulw11 For helping!

Related