选择在ios swift中的数组中的uitableview部分中取消选择收音机

sar*_*nar 4 radio-button tableview sections swift

在tableview中有不同的部分。

要为所有部分添加单选按钮。

每个部分在表格视图中都有单独的选择和取消选择。

在第一部分的选择1中,[如图所示]

选中的奶酪表示要选择奶酪,如果用户单击培根,则表示奶酪自动取消选择。

[这里使用单选按钮SSRadioButton类进行单击操作。在表格视图单元格中创建一个单选按钮。如何编写单选按钮的按钮动作。或建议任何新方法]。

每个单选按钮都需要单独选择和取消选择。表格视图中所有部分的处理相同。怎么可能帮助我。谢谢前进。在此处输入图片说明

我的代码:

 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..
}
Run Code Online (Sandbox Code Playgroud)

Rah*_*mar 5

您可以使用UIButton代替SSRadioButton,然后可以更改选中和未选中单选按钮的按钮图像。

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
        }
    }
}

}
Run Code Online (Sandbox Code Playgroud)

从UITableViewDataSource的委托方法调用initCellItem方法:

// 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
}
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明