【发布时间】:2018-05-27 11:34:04
【问题描述】:
我有一个名为 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
}
【问题讨论】:
标签: swift xcode uitableview checkbox uibutton