2016-12-13 16 views
0

私は、カスタムセルクラスを使用して、迅速にテーブルビューを作成しています。アクションボタンでカスタムセルに値を入力しようとしています

各セルにそのセルに固有のIBAction機能を実行するボタンがあるように、ボタンの配列を操作する必要があります。ここで

は、ボタンの変数を宣言する私のセルクラスです:

class taskCell: UITableViewCell { 
    @IBOutlet weak var performTask: UIButton! 
    //ignoring code irrelevant to question 
} 

私はボタンが押されたときに発生します私の「タスク」のすべてが、次のパターンのバリエーションです:

@IBAction func task1(sender: AnyObject){ 
    if (prerequisite1 >= 1) { 
     prerequisite1 = prerequisite1 - 1; 
     points = points + 400; 
     prerequisite1Label.text = "\(prerequisite1)"; 
     pointsLabel.text = "\(points)"; 
    } 
} 

@IBAction func task2(sender: AnyObject){ 
     if (prerequisite2 >= 1) { 
      prerequisite2 = prerequisite2 - 1; 
      points = points + 600; 
      prerequisite1Label.text = "\(prerequisite2)"; 
      pointsLabel.text = "\(points)"; 
     } 
    } 

ここに私のviewControllerの配列にそれらのテーブルを配置しようとしています:

let firstTasks:[()->()] = [tasksViewController().task1(), tasksViewController().task2()] 

最後に、テーブルビューで実装する実装を試みました。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath:  NSIndexPath) -> UITableViewCell { 

    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! JobCell 
     cell.performTask.tag = indexPath.row 
     cell.performTask.addTarget(self, action: firstTasks[indexpath.row], forControlEvents: .TouchUpInside)  

     return cell 
    } 
+1

それを複雑にする必要はありませんが、ジュースは、このようなcellforrow方法でタグを割り当てますindexpath.rowを呼び出して、あなたのメソッドが呼び出されるときに、sender.tag == yourvalueとしてarguemnt(送信者)をチェクすることができます。 –

+0

私はまだこのことを暗示しています。私はまだperformTaskを関数のタスクにリンクすることはできません –

+0

私はpuck me、どこにいらっしゃいましたか、私はあなたを助けます –

答えて

1

関数配列を保持する代わりに、 ViewControllerで

class JobCell: UITableViewCell { 
    @IBOutlet weak var performTask: UIButton! 
    var actionBlock: (sender: AnyObject?) -> Void?   

    func someFunc() { 
     performTask. addTargetself, action: #selector(JobCell.didTapButton(_:)), forControlEvents: .TouchUpInside) 
    } 

    func didTapButton(sender: AnyObject) { 
     actionBlock?(sender) 
    } 
} 

:なぜ、あなたはこのような何かしていない= cell.performtask.tag:このため

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! JobCell 
    cell.actionBlock = {(sender) in 
     // do ur thing 
    } 
    return cell 
} 
関連する問題