私はチェックリストアプリケーションを作成していて、クリックした後にイメージ自体が設定されたボタンイベントを更新しようとしています。私はがと未チェックを確認した後に画像を設定ボタンの両方のイベントを更新したいと思いますSwiftのボタンイベントを更新しますか?
protocol CustomeCellDelegate {
func cell(cell: UITableViewCell, updateTheButton button: UIButton)
}
class Cells: UITableViewCell, UITextFieldDelegate {
@IBOutlet weak var checkBox: UIButton!
var buttonPressed = true
@IBAction func checkBoxAction(_ sender: UIButton) {
if buttonPressed {
sender.setImage(UIImage(named: "checkBoxB"), for: UIControlState.normal)
buttonPressed = false
} else {
sender.setImage(UIImage(named:""), for: UIControlState.normal)
buttonPressed = true
}
}
@objc func recordButtonImage(_ button: UIButton) {
cellDelegate?.cell(cell: self, updateTheButton: button) // cell notify the button that touched.
}
override func awakeFromNib() {
checkBox.addTarget(self, action: #selector(recordButtonImage(_:)), for: UIControlEvents.touchUpInside)
}
}
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate, CustomeCellDelegate {
@IBOutlet weak var tableView: UITableView!
var myImages: [UIImage?]!
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.delegate = self
self.tableView.dataSource = self
myImages = Array(repeatElement(nil, count: 50))
let nib = UINib(nibName: "Cells", bundle: nil)
tableView.register(nib, forCellReuseIdentifier: "Cells")
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 50
}
internal func tableView(_ tableView:UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "theCells") as? Cells
cell?.theTextField.delegate = self
cell?.cellDelegate = self
cell?.checkBox.setImage(myImages[indexPath.row], for: UIControlState.normal)
return cell!
}
// This function from protocol and it helps to update button images.
func cell(cell: UITableViewCell, updateTheButton button: UIButton) {
print("Delegate method Update Button Images.")
if let indexPath = tableView.indexPath(for: cell), let buttonImage = button.image(for: UIControlState.normal) {
myImages[indexPath.row] = buttonImage
}
}
:
は、ここに私のコードです。したがって、私のテーブルビューは再利用可能なセルをデキューし、それらのイベントを更新することができます。
しかし、チェックされたボタンのイベントは1つだけですが、チェックされていないイベントはありません。チェックされたボタンは、チェックを外すために何をしても繰り返されます。
を設定しませんか。 –
@InderKumarRathoreこんにちは、私はそのステートメントを編集して、それが私の質問をより明確にすることを願っています。時間をいただきありがとうございます。 –
あなたの 'checkBoxAction'アクションは' checkBox'ボタンで接続されています –