0

I have tableview with custom cells with multiple sections. There are multiple rows in every section. Each row contains one label and one button(radio). I have created IBOutlet action of the button. My requirement is to select only one label in each section. To resolve this I have implemented below code. Another issue is that if I select one radio button and scroll to another section, the radio button should be shown as selected. How to resolve this issue, please tell me.

let position: CGPoint = sender.convertPoint(CGPointZero, toView: table_view)
if let indexPath = table_view.indexPathForRowAtPoint(position)
{
    let cell = (table_view.cellForRowAtIndexPath(indexPath)! as! PartCell)
    let Section_count = table_view.numberOfSections
    let rowCount = table_view.numberOfRowsInSection(indexPath.section)
    for j in 0..<Section_count
    {
       if j==indexPath.section
       {
          for i in 0..<rowCount
          {
              if i==indexPath.row
              {
                cell.btn_radio.selected=true
              }
              else
              {
                cell.btn_radio.selected=false
              }
          }
     }
 }
MLavoie
  • 9,671
  • 41
  • 36
  • 56
Gaurav Gupta
  • 593
  • 2
  • 10
  • 31
  • can't get your problem. Please explain more & in straight way. – Gagan_iOS Oct 17 '16 at 09:19
  • @Gaurav Gupta http://stackoverflow.com/a/28624956/1882271 – Darshan Kunjadiya Oct 17 '16 at 09:22
  • my problem is in a tableview only select one cell of one section at a time. if we select another cell from same section then it will be change or previous is deselect. – Gaurav Gupta Oct 17 '16 at 09:24
  • my one problem is resolved but i scroll the view then radio button is unselected again. how to resolve. – Gaurav Gupta Oct 17 '16 at 09:45
  • This is happening because the TableView's cells gets reused once it goes outside of the screen. You would need to maintain the selected cell in some array of dictionary. – Bharat Modi Oct 17 '16 at 10:07
  • An easy way is make an array of selected `IndexPaths` and just check `indexPath` is in array then selected if not then not selected. – TheTiger May 23 '18 at 10:39

1 Answers1

1

You can solve using below example code:

 var selectedArray : NSMutableArray!

  override func viewDidLoad() {
        super.viewDidLoad()
        selectedArray = NSMutableArray()
}

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
      let identifier : String = "reusecell"
      let cell : UITableViewCell = tableView.dequeueReusableCell(withIdentifier: identifier)!

 if (selectedArray? .contains(indexPath))! {
       cell.accessoryType = .checkmark;
        }
          return cell;
     }

  func clickAction(_ sender: AnyObject) {
                    let button = sender as? UIButton
                    let cell = button?.superview?.superview as?   UITableViewCell
                    let indexPath = tblview.indexPath(for: cell!)

                    selectedArray! .removeAllObjects()
                    selectedArray! .add(indexPath)
                    tblview .reloadData()
                    print(indexPath?.row)
                }
KKRocks
  • 8,222
  • 1
  • 18
  • 84