Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select deselect the radio in uitableview section with array in ios swift

In tableview have different section.

Want to add the radio button for all the section.

Each section have individual select and deselect in tableview.

In first section choice1,[show in fig]

Selected cheese means cheese want to select, next if user click bacon means cheese automatically deselect.

[Here using radio button SSRadioButton class for click action. Create a radio button in tableview cell. how to write the button action for radio button. or suggest any new way].

Each radio button want individual select and deselect. The same process for all the section in tableview. how is possible help me. Thanks advance.enter image description here

my code:

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

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

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // Return the number of rows in the section.
        return table_data[section].menu_id.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)
            radioControllerChoice.shouldLetDeSelect = false
        case 3:
            radioControllerDip.addButton(cell.radioBtn)
            radioControllerDip.shouldLetDeSelect = false

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


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

            }
        }
    }

func didSelectButton(selectedButton: UIButton?)
{
        /// need solution for button action help me..
}
like image 576
saravanar Avatar asked Aug 19 '17 07:08

saravanar


1 Answers

You can use UIButton instead of SSRadioButton, and then you can change the image of button for checked and unchecked radio button.

Swift3.2: CustomiseTableViewCell

import UIKit

protocol CustomTableViewCellDelegate {
    func didToggleRadioButton(_ indexPath: IndexPath)
}

class CustomiseTableViewCell: UITableViewCell {

@IBOutlet weak var itemLabel: UILabel!
@IBOutlet weak var radioButton: UIButton!
var delegate: CustomTableViewCellDelegate?

func initCellItem() {

    let deselectedImage = UIImage(named: "ic_radio_button_unchecked_white")?.withRenderingMode(.alwaysTemplate)
    let selectedImage = UIImage(named: "ic_radio_button_checked_white")?.withRenderingMode(.alwaysTemplate)
    radioButton.setImage(deselectedImage, for: .normal)
    radioButton.setImage(selectedImage, for: .selected)
    radioButton.addTarget(self, action: #selector(self.radioButtonTapped), for: .touchUpInside)
}

func radioButtonTapped(_ radioButton: UIButton) {
    print("radio button tapped")
    let isSelected = !self.radioButton.isSelected
    self.radioButton.isSelected = isSelected
    if isSelected {
        deselectOtherButton()
    }
    let tableView = self.superview as! UITableView
    let tappedCellIndexPath = tableView.indexPath(for: self)!
    delegate?.didToggleRadioButton(tappedCellIndexPath)
}

func deselectOtherButton() {
    let tableView = self.superview?.superview as! UITableView
    let tappedCellIndexPath = tableView.indexPath(for: self)!
    let indexPaths = tableView.indexPathsForVisibleRows
    for indexPath in indexPaths! {
        if indexPath.row != tappedCellIndexPath.row && indexPath.section == tappedCellIndexPath.section {
            let cell = tableView.cellForRow(at: IndexPath(row: indexPath.row, section: indexPath.section)) as! CustomiseTableViewCell
            cell.radioButton.isSelected = false
        }
    }
}

}

Call initCellItem method from UITableViewDataSource's delegate method:

// Your ViewController

let menuList = [ ["Cheese", "Bacon", "Egg"],
["Fanta", "Lift", "Coke"] ]  // Inside your ViewController
var selectedElement = [Int : String]()

func didToggleRadioButton(_ indexPath: IndexPath) {
    let section = indexPath.section
    let data = menuList[section][indexPath.row]
    if let previousItem = selectedElement[section] {
        if previousItem == data {
            selectedElement.removeValue(forKey: section)
            return
        }
    }
    selectedElement.updateValue(data, forKey: section)
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
   let cell:CustomiseTableViewCell = 
   tableView.dequeueReusableCell(withIdentifier: "Customise") as! CustomiseTableViewCell
    let item = menuList[indexPath.section][indexPath.row]
    cell.itemLabel.text = item
    if item == selectedElement[indexPath.section] {
        cell.radioButton.isSelected = true
    } else {
        cell.radioButton.isSelected = false
    }
    cell.initCellItem()
    cell.delegate = self
    // Your logic....
    return cell
}

enter image description here

like image 87
Rahul Kumar Avatar answered Nov 15 '22 08:11

Rahul Kumar