UiTableViewCell single selected checkmark for each section in the tableview

How to make checkmark single selection to Each Section , with custom cells , when I realize this code , cell doesn't marked

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        guard let it = PayMentSection(rawValue: indexPath.section) else {return}
        switch it {
        case .delivering:
            let cell = tableView.dequeueReusableCell(withIdentifier: cellid) as! MakePaymentTableViewCell
            cell.checkBox.isChecked = true
            cell.accessoryType = .checkmark
        case .ordering:
            let cell2 = tableView.dequeueReusableCell(withIdentifier: cellid2) as! MakePaymentTableViewCell2
            cell2.checkBox.isChecked = true

        }

    }

    func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        guard let it = PayMentSection(rawValue: indexPath.section) else {return}
        switch it {
        case .delivering:
            let cell = tableView.dequeueReusableCell(withIdentifier: cellid) as! MakePaymentTableViewCell
          cell.checkBox.isChecked = false
            cell.accessoryType = .none
        case .ordering:
            let cell2 = tableView.dequeueReusableCell(withIdentifier: cellid2) as! MakePaymentTableViewCell2
          cell2.checkBox.isChecked = false

            cell2.accessoryType = .none
        }
    }

Upvotes: 3

Views: 828

Answers (2)

Vishvesh Pratap Singh
Vishvesh Pratap Singh

Reputation: 21

You try can this method:

Create variables as a type of IndexPath (like you have 3 sections create 3 variables)

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        switch indexPath.section {
            case 0:
                if let previousIndexPath = selectedValueIndexPath {
                    tableView.cellForRow(at: previousIndexPath)?.accessoryType = .none
                }
                selectedValueIndexPath = indexPath
                tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
                
            case 1:
                if let previousIndexPath = selectedOrderIndexPath {
                    tableView.cellForRow(at: previousIndexPath)?.accessoryType = .none
                }
                selectedOrderIndexPath = indexPath
                tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
                
            default:
                break
        }
    }

Upvotes: 0

PGDev
PGDev

Reputation: 24341

First of all, implement UITableViewDelegate's tableView(_:willSelectRowAt:) method like so,

func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
    if let selectedIndexPathsInSection = tableView.indexPathsForSelectedRows?.filter({ $0.section == indexPath.section }), !selectedIndexPathsInSection.isEmpty {
        selectedIndexPathsInSection.forEach({ tableView.deselectRow(at: $0, animated: false) })
    }
    return indexPath
}

Next, in both MakePaymentTableViewCell and MakePaymentTableViewCell2 cells, override setSelected(_:animated:) method, i.e.

class MakePaymentTableViewCell: UITableViewCell {
    //rest of the code...
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        self.accessoryType = selected ? .checkmark : .none
        self.checkBox.isChecked = selected
    }
}

There is no need to implement didSelectRowAt and didDeselectRowAt methods.

enter image description here

Upvotes: 3

Related Questions