saravanar saravanar - 1 year ago 91
Swift Question

Tableview multiple sections button action for radio btn and checkbox in swift

In my application there are multiple sections namely checkbox and radio button.

In the tableview cell I created two UIButton, which changes depending on the response, if its checkbox or radio button.

here is my code.

var radioControllerChoice : SSRadioButtonsController = SSRadioButtonsController()
var radioControllerDip : SSRadioButtonsController = SSRadioButtonsController()
var radioControllerDrink : SSRadioButtonsController = SSRadioButtonsController()
var radioControllerSides : SSRadioButtonsController = SSRadioButtonsController()
var radioControllerOption : SSRadioButtonsController = SSRadioButtonsController()

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


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:CustomiseTableViewCell = tableView.dequeueReusableCell(withIdentifier: "Customise") as! CustomiseTableViewCell
cell.name.text?=table_data[indexPath.section].menu_name[indexPath.row]
print(table_data[indexPath.section].customize[indexPath.row])

switch indexPath.section {
case 2:
radioControllerChoice.addButton(cell.radioBtn)
case 3:
radioControllerDip.addButton(cell.radioBtn)
case 4:
radioControllerDrink.addButton(cell.radioBtn)
case 5:
radioControllerSides.addButton(cell.radioBtn)
case 6:
radioControllerOption.addButton(cell.radioBtn)
default:
print("no case found")
}

switch Int(table_data[indexPath.section].customize[indexPath.row]) {
case 1:
cell.radioBtn.isHidden = true
cell.checkBoxBtn.isHidden = false
break
case 2:
cell.radioBtn.isHidden = false
cell.checkBoxBtn.isHidden = true
break
default:
print("Invalid choose")

}
cell.radioBtn.addTarget(self, action: #selector(ViewController.didSelectButton), for: .touchUpInside)
cell.radioBtn.tag = indexPath.row
cell.checkBoxBtn.addTarget(self, action: #selector(ViewController.checkBoxBtnaction), for: .touchUpInside)
return cell

}


table_data is the array, and I get the array values from web services using struct.

My problem is:

write the button action for radio button.

func didSelectButton(selectedButton: UIButton?)
{
print("selectedButton",(selectedButton?.tag)!)
let tagVal = (selectedButton?.tag)!
print("tagVal",tagVal)

}


In button click action choice is one separate sections in tableview show in image. In that sections want to select and deselect the radio button. when select cheese it will selected, then I will select the triple layer cheese means, cheese automatically deselect. want to select only one item in that sections.

Each sections have a different item with radio button.

If selected one radio button it will select, after selected any other radio button means the pervious selected button want to deselect automatically. It should happen in each sections in tableview only for radio button.


whatever the item selected want to store it in string.

The same button action need for check box with multiple selection.

func checkBoxBtnaction(sender:UIButton)
{


}


enter image description here

enter image description here

Help me. Thanks in advance.

Answer Source

Use this library for radio buttons and update your code with this and try

    var radioButtonController = SSRadioButtonsController()

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell:CustomiseTableViewCell = tableView.dequeueReusableCell(withIdentifier: "Customise") as! CustomiseTableViewCell
            cell.name.text?=table_data[indexPath.section].menu_name[indexPath.row]
            print(table_data[indexPath.section].customize[indexPath.row])

            radioButtonController.addButton(cell.radioBtn)

            switch Int(table_data[indexPath.section].customize[indexPath.row]) {
                case 1:
                    cell.radioBtn.isHidden = true
                    cell.checkBoxBtn.isHidden = false
                    break
                case 2:
                    cell.radioBtn.isHidden = false

                    cell.checkBoxBtn.isHidden = true
                    break
                default:
                    print("Invalid choose")

                }
                cell.radioBtn.addTarget(self, action: #selector(ViewController.radioBtnaction), for: .touchUpInside)
                cell.checkBoxBtn.addTarget(self, action: #selector(ViewController.checkBoxBtnaction), for: .touchUpInside)

                return cell

            }

EDIT:

var radioControllerChoice : SSRadioButtonsController = SSRadioButtonsController()
var radioControllerDip : SSRadioButtonsController = SSRadioButtonsController()

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell:CustomiseTableViewCell = tableView.dequeueReusableCell(withIdentifier: "Customise") as! CustomiseTableViewCell
    cell.name.text?=table_data[indexPath.section].menu_name[indexPath.row]
    print(table_data[indexPath.section].customize[indexPath.row])


    switch indexPath.section {
    case 1:
        radioControllerChoice.addButton(cell.radioBtn)
    case 2:
        radioControllerDip.addButton(cell.radioBtn)
    default:
        print("no case found")
    }
    switch Int(table_data[indexPath.section].customize[indexPath.row]) {
    case 1:
        cell.radioBtn.isHidden = true
        cell.checkBoxBtn.isHidden = false
        break
    case 2:
        cell.radioBtn.isHidden = false

        cell.checkBoxBtn.isHidden = true
        break
    default:
        print("Invalid choose")

    }
    cell.radioBtn.addTarget(self, action: #selector(ViewController.radioBtnaction), for: .touchUpInside)
    cell.checkBoxBtn.addTarget(self, action: #selector(ViewController.checkBoxBtnaction), for: .touchUpInside)

    return cell

}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download