这是我想要实现的:
填充所有数据没有问题,但问题是如何让用户只为单选部分选择一行,并为复选框部分允许多行,我被困在这一部分。这是现在的代码:
@objc func checkboxSelected(_ sender: UIButton) {
sender.isSelected = !sender.isSelected
if(sender.isSelected == true)
{
sender.setImage(UIImage(named: "red_checkbox"), for: UIControlState.normal)
}
else
{
sender.setImage(UIImage(named: "checkbox2"), for: UIControlState.normal)
}
}
@objc func radioBtnSelected(_ sender: UIButton) {
sender.isSelected = !sender.isSelected
if(sender.isSelected == true)
{
sender.setImage(UIImage(named: "radio_red"), for: UIControlState.normal)
}
else
{
sender.setImage(UIImage(named: "radio_white"), for: UIControlState.normal)
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "DishTableViewCell", for: indexPath) as! DishTableViewCell
cell.titleLbl.text = self.menu[indexPath.section].menuOptions[indexPath.row].name
if self.menu[indexPath.section]. menuOptions[0].title == "checkbox" {
cell.checkboxBtn.isHidden = false
cell.radioBtn.isHidden = true
}else if
self.menu[indexPath.section]. menuOptions[0].title == "radio" {
cell.checkboxBtn.isHidden = true
cell.radioBtn.isHidden = false
}
cell.checkboxBtn.addTarget(self, action: #selector(DishViewController.checkboxSelected(_:)), for: UIControlEvents.touchUpInside)
cell.radioBtn.tag = self.menu[indexPath.section]. menuOptions[indexPath.row].id
cell.radioBtn.addTarget(self, action: #selector(DishViewController.radioBtnSelected(_:)), for: UIControlEvents.touchUpInside)
return cell
}
或者除了使用表格视图之外还有其他方法可以做吗?请协助。谢谢
我建议您使用UIImageView而不是按钮并使用tableView didSelectRowAt 方法。
我已经编辑了您的代码并在下面进行了一些更改:
1.声明两个变量来跟踪索引路径
var radioButtonIndexPath = [Int:IndexPath]() //for radiobutton
var checkboxIndexPath = [indexPath]() //for checkbox
2.cellForRowAt 方法已使用 UIImageView 而不是 UIButton 进行了修改
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "DishTableViewCell", for: indexPath) as! DishTableViewCell
cell.titleLbl.text = self.menu[indexPath.section].menuOptions[indexPath.row].name
if self.menu[indexPath.section]. menuOptions[0].title == "checkbox" {
cell.checkbox.isHidden = false
cell.radio.isHidden = true
if checkboxIndexPath.contains(IndexPath) {
checkbox.image = UIImage(named:"red_checkbox")
}else{
checkbox.image = UIImage(named:"checkbox2")
}
}else if
self.menu[indexPath.section]. menuOptions[0].title == "radio" {
cell.checkbox.isHidden = true
cell.radio.isHidden = false
if radioButtonIndexPath.keys.contains(indexPath.section) {
if radioButtonIndexPath[indexPath.section] == indexPath {
radio.image = UIImage(named:"radio_red")
}else{
radio.image = UIImage(named:"radio_white")
}
}else{
radio.image = UIImage(named:"radio_white")
}
}
return cell
}
3.选择行方法:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if self.menu[indexPath.section].menuOptions[0].title == "checkbox" {
if checkboxIndexPath.contains(IndexPath) {
checkboxIndexPath.remove(at: checkboxIndexPath.index(of: IndexPath))
}else{
checkboxIndexPath.append(IndexPath)
}
}else if self.menu[indexPath.section].menuOptions[0].title == "radio" {
radioButtonIndexPath[indexPath.section] = indexPath
}
yourtableView.reloadData() // reload your tableview here
}
尝试此代码,如果有任何问题,请告诉我。