TableView有多个部分,带有单选按钮和复选框按钮

时间:2018-01-10 08:51:07

标签: swift tableview

这就是我想要实现的目标:

填充所有数据没有问题,但问题是如何让用户只选择一行用于无线电部分,并允许多个行用于复选框部分,我被困在这一部分。这是现在的代码:

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

或者除了使用tableview之外还有其他方法吗?请协助。谢谢

1 个答案:

答案 0 :(得分:1)

我建议您使用UIImageView代替按钮并使用tableView didSelectRowAt方法。

我已编辑您的代码并在下面进行了一些更改:

1.Declare两个变量用于跟踪索引路径

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.didSelectRowAt方法:

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 
}

尝试此代码,如果有任何问题,请告诉我。