gpt4 book ai didi

swift - 快速查看单选按钮和复选框的多个部分按钮操作

转载 作者:行者123 更新时间:2023-11-28 10:52:02 25 4
gpt4 key购买 nike

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

在 tableview 单元格中,我创建了两个 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 是数组,我使用结构从 Web 服务获取数组值。

我的问题是:

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

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

}

在按钮点击 Action 选择中,表格 View 中的一个单独部分显示在图像中。 在那些想要选择和取消选择单选按钮的部分。 when select cheese it will selected, then I will select the triple layer cheese means, cheese automatically deselect.只想在该部分中选择一个项目。

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

无论选择什么项目,都希望将其存储在字符串中。

具有多个选择的复选框需要相同的按钮操作。

func checkBoxBtnaction(sender:UIButton)
{


}

在此输入图片描述

enter image description here

帮帮我。提前致谢。

最佳答案

使用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

}

关于swift - 快速查看单选按钮和复选框的多个部分按钮操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45710751/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com