0

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.

Revanth Kausikan
  • 673
  • 1
  • 9
  • 21
Naresh
  • 16,698
  • 6
  • 112
  • 113
  • Why won't you use button with states? I am very uncleared with your question. – dahiya_boy Apr 09 '19 at 09:16
  • can you show this how do u crteated this `selectedIndexPath` – Anbu.Karthik Apr 09 '19 at 09:18
  • No use of buttons, because of we have imageView to show image and didSelectrow at index path function for selection. – Naresh Apr 09 '19 at 09:19
  • @Anbu.Karthik, var selectedIndexPath:IndexPath? created globally – Naresh Apr 09 '19 at 09:19
  • 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 Apr 09 '19 at 09:20
  • @ dahiya_boy, I think button also will get the same problem, because of I'm using multiple sections here. For single section it's ok, for multiple sections how to add tags. – Naresh Apr 09 '19 at 09:22
  • @ Anbu.Karthik, hay dear Anbu.Karthik can you please solve my one more question https://stackoverflow.com/questions/55373430/twitter-error-your-credentials-do-not-allow-access-to-this-resource/55539202#55539202 – Naresh Apr 09 '19 at 09:23
  • @ dahiya_boy, dear dahiya_boy can you please solve my one more question https://stackoverflow.com/questions/55373430/twitter-error-your-credentials-do-not-allow-access-to-this-resource/55539202#55539202 – Naresh Apr 09 '19 at 09:23
  • @iOS why are you complicating yourself? why dont you just use all rows? and show it like its group of sections and rows in UI? – Harjot Singh Apr 09 '19 at 09:43
  • @ Harjot Singh, I'm using sections. It has 3 sections – Naresh Apr 09 '19 at 09:45
  • @iOS why don't you use all rows? – Harjot Singh Apr 09 '19 at 09:46
  • @ Harjot Singh, no those are different types – Naresh Apr 09 '19 at 09:48
  • @iOS different types? That you know but not your code, you can use all rows and differentiate it based on your requirements. – Harjot Singh Apr 09 '19 at 09:49
  • @ dahiya_boy actually if I use only buttons user need to tap only on buttons. This is filter View, so size of buttons is so small. That's why I'm given selection for rows. – Naresh Apr 09 '19 at 10:25

1 Answers1

2

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
RajeshKumar R
  • 15,445
  • 2
  • 38
  • 70