mardi 13 mars 2018

Single Selection of an item in TableView

I have a tableView, which is expandable. The headerCell has a checkBox, a label and a radioButton. The collapsableCell has a checkBox and a label.

I have used M13Checkbox Library to implement the checkBox and the radioButton.

The problem is when I select a radioButton or a checkBox of HeaderViewCell at index 0, then the radioButton/checkBox at index 8,16,24 also get selected. I know it is because of the numberOfSections in tableView property, But then how do I select just single radioButton at a time. My requirement is I have to select single radioButton in the tableView and CheckBoxes can have multiple selections for the HeaderCell.

I am badly stuck on this issue. I have googled a lot but nothing worked. Any help or suggestion much appreciated.

func numberOfSections(in tableView: UITableView) -> Int {
    return states.count
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return states[section].cities.count
}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 50.0
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if (states[indexPath.section].expanded) {
        return 44
    }else{
        return 0.0
    }
}

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let headerCell = tableView.dequeueReusableHeaderFooterView(withIdentifier: "headerviewcell") as! HeaderView
    var list = states[section]
    headerCell.customInit(titleLabel: list.stateName, section: section, delegate: self)
    return headerCell
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "subcells") as! CollapsibleCell
    cell.selectionStyle = .none   
    cell.textLabel?.text = states[indexPath.section].cities[indexPath.row]
    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    self.selectIndexPath = indexPath

    let subCell = tableView.cellForRow(at: indexPath) as! CollapsibleCell

    if selectIndexPath.row == indexPath.row {
        subCell.checkBox.isSelected = true
    }else{
        subCell.checkBox.isSelected = false
    }
    tableView.deselectRow(at: indexPath, animated: false)
}

func toggleHeader(header : Medical_Cond_HeaderView, section : Int){
    states[section].expanded = !states[section].expanded

    tableView.beginUpdates()
    for i in 0 ..< states[section].cites.count {
        tableView.reloadRows(at: [IndexPath(row: i, section: section)], with: .automatic)
    }
    tableView.endUpdates()
}




Aucun commentaire:

Enregistrer un commentaire