Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift UITableView复选框(按下另一个按钮时选择一个按钮)_Swift_Xcode_Uitableview_Checkbox_Uibutton - Fatal编程技术网

Swift UITableView复选框(按下另一个按钮时选择一个按钮)

Swift UITableView复选框(按下另一个按钮时选择一个按钮),swift,xcode,uitableview,checkbox,uibutton,Swift,Xcode,Uitableview,Checkbox,Uibutton,我有一个名为btnChk2的按钮。我希望当用户按下btnChk2时,按钮btnChk被选中。这是我的代码在我的代码中发生的是,当按下btnChk2时,btnChk2被选中,而不是btnChk func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIden

我有一个名为btnChk2的按钮。我希望当用户按下btnChk2时,按钮btnChk被选中。这是我的代码在我的代码中发生的是,当按下btnChk2时,btnChk2被选中,而不是btnChk

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "CheckBoxCell")

    if let lbl = cell?.contentView.viewWithTag(1) as? UILabel {
        lbl.text = "item-\(1)"
    }

    if let btnChk = cell?.contentView.viewWithTag(2) as? UIButton {
        btnChk.addTarget(self, action: #selector(checkboxClicked(_ :)), for: .touchUpInside)
    }

    if let btnChk2 = cell?.contentView.viewWithTag(100) as? UIButton {
        btnChk2.addTarget(self, action: #selector(checkboxClicked(_ :)), for: .touchUpInside)
    }

    return cell!
}

@objc func checkboxClicked(_ sender: UIButton) {
    sender.isSelected = !sender.isSelected
}

您可以处理比这更好的工作,您必须在
cell
CellforRow
仅管理状态中执行此逻辑

你能那样做吗

 @objc func checkboxClicked(_ sender: UIButton) {

        guard let cell = sender.superview?.superview as? UITableViewCell else {
            return
        }

        if  sender.tag == 2 {

            if let btnChk2 = cell.contentView.viewWithTag(100) as? UIButton {
                if btnChk2.isSelected == true {
                    btnChk2.isSelected = false

                }else{
                    btnChk2.isSelected = true

                }
            }
            sender.isSelected = false
        }else if sender.tag == 100{
            if let btnChk = cell.contentView.viewWithTag(2) as? UIButton {
                if btnChk.isSelected == true {
                    btnChk.isSelected = false

                }else{
                    btnChk.isSelected = true
                }
            }
        }
    }

尝试了您的代码,但当我按下btnChk2时,btnChk2被选中,但当我按下btnChk2以选中btnChk时,我想要我。工作刚刚更改,但当我按下按钮时,不会被取消选中您需要什么,当按2时选择一个,当我添加
btnChk2.isSelected=!btnChk2.isSelected
按钮停止被选中。下面是代码
if sender.tag==2{if let btnChk2=cell.contentView.viewWithTag(100)as?UIButton{btnChk2.isSelected=true btnChk2.isSelected=!btnChk2.isSelected}}否则if sender.tag==100{if let btnChk=cell.contentView.viewWithTag(2)as?UIButton{btnChk.isSelected=true btnChk.isSelected=!btnChk.isSelected}}
您需要什么,当按2时,选择一个,当按1时,如果选择要删除-Abdelahad Darwish 4分钟前编辑