0
votes

I have 3 sections in my tableView, I want single selection like radio buttons in each section. My code working for single section, but not for multiple sections. I'm not using any custom buttons for this, directly using contentView imageView to set images and didSelectRowAt indexPath: for selection

My code is

cell?.imageView?.image = UIImage.init(named: "radio1")//Deselected state
cell?.imageView?.tag = indexPath.row
cell?.contentView.viewWithTag(8888)
if (selectedIndexPath != nil && selectedIndexPath?.section == 0 && selectedIndexPath?.row == indexPath.row) {
    cell?.imageView?.image = UIImage.init(named: "radio2")//Selected state
} else {
    cell?.imageView?.image = UIImage.init(named: "radio1")//Deselected state
}
if selectedIndexPath?.section == 1 {
}
if selectedIndexPath?.section == 2 {
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    selectedIndexPath = indexPath//Save selected indexpath
    print(selectedIndexPath)
    tblView.reloadData()

The above code getting result like

When I select (Section1, Row0) automatically (Section2, Row0)also selected. I want select rows individually based on sections.

1
Why won't you use button with states? I am very uncleared with your question. - dahiya_boy
can you show this how do u crteated this selectedIndexPath - Anbu.Karthik
No use of buttons, because of we have imageView to show image and didSelectrow at index path function for selection. - iOS
@Anbu.Karthik, var selectedIndexPath:IndexPath? created globally - iOS
What is the issue .. changing image? or not able to select multi section.. button also have image property .. for radio button..use uIbutton bcz its more handy. - dahiya_boy

1 Answers

2
votes

For a single section tableview you can use selectedIndexPath to store selected indexPath. For multiple section tableview you need an array of indexPaths

var selectedIndexPathArr: [IndexPath?] = Array(repeating: nil, count: 3)//3 is number of sections

Then in didSelect method store selected indexpath in its section index

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    selectedIndexPathArr[indexPath.section] = indexPath//save selected indexpath depends section
    tableView.reloadData()
}

In didDeselectRowAt method remove seaved index path from the array

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    selectedIndexPathArr[indexPath.section] = nil//delete selected indexpath depends section
    tableView.reloadData()
}

In cellForRowAt method compare current indexpath with the indexpath from the array depends on the current section

if selectedIndexPathArr[indexPath.section] == indexPath {
    cell?.imageView?.image = UIImage.init(named: "radio2")//Selected state
} else {
    cell?.imageView?.image = UIImage.init(named: "radio1")//Deselected state
}

enter image description here

Then you can get selected indexpath for each section

You'll get nil if no cell is selected in particular section

print(selectedIndexPathArr[0])//first section selected indexpath
print(selectedIndexPathArr[1])//second section selected indexpath
print(selectedIndexPathArr[2])//third section selected indexpath