【问题标题】:TableView with multiple section and with radio button and checkbox button具有多个部分并带有单选按钮和复选框按钮的 TableView
【发布时间】:2018-06-19 09:25:19
【问题描述】:

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

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

@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之外还有其他方法吗?请协助。谢谢

【问题讨论】:

  • 这会有帮助吗? stackoverflow.com/a/2268673/366346
  • 最好配合tableview didselect方法使用
  • @Shezad 怎么用?因为会有不止一个部分,意味着里面有多行的无限部分。
  • @GoodSp33d 不,刚刚尝试过。不工作
  • @mimi93 即使有多个部分,您也只有两种类型的选择,对吗? (复选框或单选按钮)。使用索引路径数组来跟踪选定的索引路径。

标签: swift tableview


【解决方案1】:

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

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

1.声明两个变量来跟踪indexpath

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 
}

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

【讨论】:

  • 它有效,但如果我有多个单选部分的部分,它将不允许我选择其他部分的行,它只允许我在里面的所有部分中选择一个单选按钮(对于广播部分)。
  • @mimi93 我已经编辑了代码,请检查它,如果有任何问题,请告诉我。
  • @mimi93 我已将 radioButtonIndexPath 更改为字典,因此答案的所有三个部分都发生了变化,所以只需检查一下
  • 当取消选中所有复选框时,它会在“checkboxIndexPath.remove(at:IndexPath)”行崩溃。是不是和广播科一样的原因?因为复选框部分也会有不止一个部分。
  • 我刚刚上传了错误的新截图,请看上图。当我试图取消选中复选框并崩溃时会发生这种情况
猜你喜欢
  • 1970-01-01
  • 2017-07-28
  • 2015-11-12
  • 1970-01-01
  • 2021-05-19
  • 1970-01-01
  • 2015-11-02
  • 2012-06-05
  • 1970-01-01
相关资源
最近更新 更多