【问题标题】:Select deselect the radio in uitableview section with array in ios swift在 ios swift 中使用数组在 uitableview 部分中选择取消选择收音机
【发布时间】:2017-08-19 07:43:28
【问题描述】:

在 tableview 中有不同的部分。

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

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

在第一部分choice1,[如图所示]

Selected cheese 表示要选择的奶酪,下一步如果用户点击 bacon 表示奶酪自动取消选择。

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

每个单选按钮都需要单独选择和取消选择。表格视图中所有部分的相同过程。怎么可能帮助我。谢谢提前。

我的代码:

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

【问题讨论】:

  • 最好包含代码的相关部分,例如 cellForRowdidSelectRow 方法以及您的数据源,以便我们帮助您解决问题
  • 你能分享任何代码来解决这个问题吗..我正在努力..
  • 我可以给出一个通用的答案,这可能会或可能不会帮助你,但如果你用代码更新你的问题,我可能会帮助你克服这个问题
  • 更新我的问题检查一下。

标签: swift radio-button tableview sections


【解决方案1】:

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

Swift3.2: 自定义TableViewCell

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

}

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

【讨论】:

  • Selected cheese 表示要选择的奶酪,接下来如果用户点击 bacon 表示奶酪自动取消选择。每个单选按钮都需要单独选择和取消选择。表格视图中所有部分的相同过程。怎么可能帮助我...如何在 cellforrow 中编写逻辑。
  • @saravanar,我已经编辑了上面的代码来解决你的问题。现在它正在工作,请检查它。
  • 选择和取消选择单选按钮时如何在数组中添加元素[cheese,egg][fanta,lift]..
  • 其实在swift3.0中,self.superview为! UITableView 给了我 UITableViewWrapperView 所以我用 self.superview?.superview 作为! UITableView 正在工作。对于您的代码,您可以使用 let tableView = self.superview as! UITableView
  • 如何在tableview的didselect方法中使用相同的代码来实现对tableview的完整单元格的检查和取消检查
【解决方案2】:

替代方式:

您可以使用简单的UIButton 代替任何第三方库 (SSRadioButton) 并像这样使用它:

  1. default state中的UIButton'simage设置为-圆圈(如屏幕截图所示)

  2. selected state 中的UIButton's image 设置为 - 实心圆

UIButton's action event 可以像在任何其他情况下一样以正常方式捕获。

类似这样的:

如果您想采用这种方法或需要任何形式的帮助,请告诉我。

【讨论】:

  • 这里使用单选按钮,因此每个部分只有一个选择选项。不能重复,如果一个单选按钮被选中,单击另一个单选按钮意味着第一个按钮自动取消选择,单击的按钮将被选中..每个按钮都有选择和取消选择选项。怎么可能。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-11-19
  • 2011-06-04
  • 1970-01-01
  • 2017-03-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多