Swift UITableView复选框(按下另一个按钮时选择一个按钮)
我有一个名为btnChk2的按钮。我希望当用户按下btnChk2时,按钮btnChk被选中。这是我的代码在我的代码中发生的是,当按下btnChk2时,btnChk2被选中,而不是btnChkSwift 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
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分钟前编辑