【发布时间】: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