2016-11-24 5 views
0

私は4つのセルを返すTableViewControllerを持っています。各セルには3つのボタンがあります。セル自体は、ユーザーがセルとやりとりする必要がないため、セル内の3つのボタンだけで、相互作用やつなぎ目が付いていません。すべてのUITableViewCellからデータを個別に取得

私はselectionsと呼ばれる空の配列を持っています。各ボタンが押されると、その配列に項目を追加します。これまで私は、押された各ボタンを追跡するメソッドを見つけることができません。

このコードはどのような方法で入力できますか?

if cell.yes.isSelected == true { 
      firstChoice = 1 
      selections.append(firstChoice) 
      print(selections.count, selections.reduce(0, +)) 
     } 

このように、私のTableViewControllerによって読み込まれたすべてのセルで動作するのでしょうか?

+0

を参照してください:http://stackoverflow.com/questions/ 20655060/get-button-click-inside-ui-table-view-cell –

答えて

1

カスタムセルとボタンの両方のデリゲートを作成する必要があります。

//here is the view controller class 
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, CustomCellDelegate { 

    var firstChoice = 0 
    var selections = Array<Any>() 

    @IBOutlet weak var tableView: UITableView! 
     override func viewDidLoad() { 
      super.viewDidLoad() 


    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as? CustomCell 

     cell?.delegate = self 

     return cell! 
    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return 4 
    } 

    func cellButtonTapped(_ cell: CustomCell) { 
     if cell.isSelected { 
      firstChoice = 1 
      selections.append(firstChoice) 
     } 
    } 
} 

カスタムセルクラス

protocol CustomCellDelegate { 
    func cellButtonTapped(_ cell: CustomCell) 
} 

class CustomCell: UITableViewCell, CustomButtonDelegate { 

    var delegate: CustomCellDelegate? 

    @IBOutlet weak var button1: CustomButton! 
    @IBOutlet weak var button2: CustomButton! 
    @IBOutlet weak var button3: CustomButton! 

    override func awakeFromNib() { 
     super.awakeFromNib() 

     button1.delegate = self 
     button2.delegate = self 
     button3.delegate = self 
    } 

    func buttonTapped() { 
     self.isSelected = !self.isSelected 

     if let delegate = delegate { 
      delegate.cellButtonTapped(self) 
     } 
    } 
} 

そしてカスタムボタンそのプロトコルと

protocol CustomButtonDelegate{ 
    func buttonTapped() 
} 

class CustomButton: UIButton { 

    var delegate: CustomButtonDelegate? 

    override func awakeFromNib() { 
     super.awakeFromNib() 

     self.addTarget(self, action: #selector(buttonTapped(_:)), for: .touchUpInside) 
    } 

    func buttonTapped(_ sender: AnyObject) { 
     if let delegate = delegate { 
      delegate.buttonTapped() 
     } 
    } 
} 

それらの両方

+0

カスタムボタンクラスが必要なのはなぜですか? –

+0

プログラムでカスタムクラスを作成する方がはるかに優れています。ここではデリゲートを使用してカスタムボタンを作成しています。そのため、ユーザーがボタンをタップするとデリゲートメソッドが呼び出され、必要なコードを記述できます。さらにここでは、4つのセルに3つのボタンがあり、どのボタンがユーザのタップを行ったかをアプリがどのように知っているのでしょうか?各ボタンの3つの別々のIBActionsをコードするのは良い方法ではありません。 –

1

まず、ViewControllerの押されたボタンに関する情報をテーブルのセル内に保存する必要があります。 表のセルは再利用され、その情報は失われます。 これを行う最善の方法は、セルとTableViewController間のカスタムデリゲートを使用することです。各セルを作成する際、あなたが作る:ボタンが押されたときに

cell.delegate = self 

とセル内部は、あなたがこのデリゲートメソッドを呼び出す - のはdidPressButton1didPressButton2をしましょう。

また、TableViewControllerでセルを作成するときに、この状態を永続化する(たとえば、無効にする、一部のボタンを有効にする)場合は、既存のデータをプルしてセル自体に適用する必要があります - TableViewCellを再利用します。

0

私はあなたの仕様を知らないが、あなたが各ボタンに別のターゲットを追加することができそうです:

button1?.addTarget(self, action:#selector(self.button1Clicked), forControlEvents: .TouchUpInside) 
... 
button3?.addTarget(self, action:#selector(self.button3Clicked), forControlEvents: .TouchUpInside) 

、その後、あなたが持っている可能性があり:

func button1Clicked() { 
    firstChoice = 1 
    selections.append(firstChoice) 
} 

を...

func button3Clicked() { 
    firstChoice = 3 
    selections.append(firstChoice) 
} 

ボタン番号1をクリックすると、button1Clicked()が発生し、意図したとおりに作業を行うことができます。

関連する問題