2017-12-08 6 views
1

私はチェックリストアプリケーションを作成していて、クリックした後にイメージ自体が設定されたボタンイベントを更新しようとしています。私はと未チェックを確認した後に画像を設定ボタンの両方のイベントを更新したいと思います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つだけですが、チェックされていないイベントはありません。チェックされたボタンは、チェックを外すために何をしても繰り返されます。

+0

を設定しませんか。 –

+0

@InderKumarRathoreこんにちは、私はそのステートメントを編集して、それが私の質問をより明確にすることを願っています。時間をいただきありがとうございます。 –

+0

あなたの 'checkBoxAction'アクションは' checkBox'ボタンで接続されています –

答えて

1

を私はあなたにドンと思う:ちょうどコードの下に置くCellForRowで

、:それはクリックされたときにボタンの

YOUR_BUTTON.isSelected = aryState[indexPath.row] as! Bool 

セット画像

YOUR_BUTTON.setImage(UIImage(named: NORMAL_IMAGE), for: .normal) 
YOUR_BUTTON.setImage(UIImage(named: SELECTED_IMAGE), for: .selected) 

はジャストボタンの状態を変更しますrecordButtonImageメソッドが必要な場合は、ボタンタップされたアクションからデリゲートメソッドを呼び出す必要があります。ちょうどまた

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 
    } 
    // cell notify the button that touched. 
    cellDelegate?.cell(cell: self, updateTheButton: button) 
    } 
} 

データソース方法下記のように。checkBoxAction、ボタンが押されたか否かのセルを教えてくれません。それはちょうどあなたが `私がチェックした後の画像を設定し、ボタンの両方のイベントを記録したいんと意味unchecked.`か説明していただけますか?画像を設定しますが、変数buttonPressed

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! 
} 
+0

それは素晴らしい仕事でした。あなたの助けを感謝Rathore、私は上記の手順を再度確認し、間違って、私は私の関数でチェックされていないボタンのための画像データを与えていないので、私は1つを作った。 –

0

処理する最善の方法UIButtonは、独自の状態を使用しています。

現在の状態値を1つの配列に保持する必要があります。だからあなたはcellForRowと他のものに状態を保持することができます。デフォルトでは、すべての状態をfalseに設定します。

@IBAction func checkBoxAction(_ sender: UIButton) { 
    let button = sender 
    button.isSelected = !button.isSelected 
} 
+0

あなたの助けを借りてNirmalsinhに感謝します、あなたの方法をチェックしました。私よりははるかに優れていますが、私のボタンの状態をセルに更新しないプロトコルです。私はそれをする方法を見つけませんでしたが、できるならば、もっと詳しく知りたいです。 –

+0

更新された回答をチェックアウトします。 – Nirmalsinh

関連する問題