【问题标题】:Tableview multiple sections button action for radio btn and checkbox in swiftswift中单选按钮和复选框的Uitableview多个部分按钮操作
【发布时间】:2017-08-16 10:10:11
【问题描述】:

在我的应用程序中有多个部分,即复选框和单选按钮。

在表格视图单元格中,我创建了两个 UIButton,根据响应(如果是复选框或单选按钮)而变化。

这是我的代码。

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 是数组,我使用 struct 从 Web 服务获取数组值。

我的问题是:

为单选按钮编写按钮操作。

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

    }

在按钮单击操作中,选择是 tableview 中显示在图像中的一个单独部分。 在该部分要选择和取消选择单选按钮。 when select cheese it will selected, then I will select the triple layer cheese means, cheese automatically deselect.只想选择该部分中的一项。

每个部分都有一个带有单选按钮的不同项目。
如果选择了一个单选按钮,它将选择,在选择任何其他单选按钮后,意味着之前选择的按钮要自动取消选择。它应该发生在 tableview 的每个部分中,仅适用于单选按钮。

无论选择的项目想要存储在字符串中。

多选复选框需要相同的按钮动作。

func checkBoxBtnaction(sender:UIButton)
 {


 }

在此处输入图片说明

帮帮我。提前致谢。

【问题讨论】:

    标签: swift checkbox radio-button tableview sections


    【解决方案1】:

    使用this 库作为单选按钮并使用它更新您的代码并尝试

        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
    
                }
    

    编辑:

    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
    
    }
    

    【讨论】:

    • SSRadioButtonsController() 在该示例中存在三个单独的按钮,我们可以将其更改为选择和取消选择。但这里一个按钮显示取决于阵列。我们如何改变它。
    • 我也为你写了代码,在那里添加了两行 var radioButtonController = SSRadioButtonsController() radioButtonController.addButton(cell.radioBtn)
    • 我又试了一次 .. 它工作得很好 .. 如果你仍然面临这个问题,你可以给我看你的代码 .. 这样我可以帮忙
    • 它工作正常,但我想让选择和取消选择选项与部分分开。第一部分选择它有 5 个单选按钮 [显示在图片中] 选择任何一个选项。如果我选择第二个单选按钮,则存在一个单选按钮,这意味着不应取消选择单选按钮。每个部分都想分开选择和取消选择。帮帮我
    • 哦,然后为每个部分创建不同的无线电控制器实例,并在 cellForRow 函数中将单选按钮添加到它们各自的无线电控制器..
    【解决方案2】:

    我会建议以下方法:

    1) 为按钮创建模型

    2) 取一个定义单选例的枚举,复选框

    3) 在单元格 UI 中创建一个普通按钮

    4) 根据枚举状态将按钮修改为单选按钮或复选框,从您的 UI 中我猜想更改按钮顶部的图像应该可以解决问题

    这样两个按钮将具有相同的操作处理程序,然后您可以根据模型枚举值决定要采取的操作

    【讨论】:

      猜你喜欢
      • 2018-06-19
      • 1970-01-01
      • 1970-01-01
      • 2011-10-18
      • 1970-01-01
      • 2013-05-10
      • 2012-12-28
      • 2023-04-06
      • 1970-01-01
      相关资源
      最近更新 更多