2016-06-17 5 views
3
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cellIdentifier = "ExerciseMenuCell" 
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! ExerciseOptionTableViewCell 
    let currentWorkout = workouts[indexPath.row] 
    cell.nameLabel!.text = currentWorkout.name 
    cell.photoImageView.image = currentWorkout.filename 

    cell.startWorkout.tag = indexPath.row 
    cell.startWorkout.addTarget(self, action:Selector("workoutAction:"), forControlEvents: .TouchUpInside) 

    cell.infoWorkout.tag = indexPath.row 
    cell.infoWorkout.addTarget(self, action:Selector("infoAction:"), forControlEvents: .TouchUpInside) 

    return cell 
    } 

両方startWorkout「認識されていないセレクタがインスタンスに送信」とアプリケーションがエラーメッセージを表示してクラッシュすることがありinfoWorkoutカスタム以内に「認識されていないセレクタはインスタンスに送信しました」。はスウィフト:UIButtonのUITableViewCellエラー

ボタンアクション内のコードの例。私はボタンのindexPathを返そうとしているので、私はその上で動作することができます。

@IBAction func workoutAction(sender: AnyObject) { 
    let buttonTag = sender.tag 
    print(buttonTag) 

} 

正確なエラーメッセージ:

016-06-17 18:34:30.722練習[4711:245683] - [Exercises.ExerciseMenu beginWorkoutは:]:認識されていないセレクタはインスタンスに送信0x7fb47874a4b0 2016から06 -17 18:34:30.727 Exercises [4711:245683] ***キャッチされていない例外 'NSInvalidArgumentException'のためアプリを終了します。理由: ' - [Exercises.ExerciseMenu beginWorkout:]:インスタンスに送信された認識できないセレクタ0x7fb47874a4b0'

+0

詳細情報:1)クラッシュログで正確なメッセージは何ですか? 2) 'workoutAction'と' infoAction'メソッドはどこで書いていますか? –

+0

メソッドは、cellForRowAtIndexPathメソッドと同じビューコントローラ内で記述されます。投稿を編集して、エラーメッセージの詳細を表示します。 –

+0

これは、必要なエラーメッセージですか? –

答えて

7

Aカスタムセル内のボタンは、ハウジングビューコントローラ内にあるアクションを呼び出すことはできません。以下を行う必要があります。

1)カスタムセルクラス

2に@IBaction機能を移動します)「「cellFromRowAtIndexPath」から、追加対象のコードを削除し、いずれかのカスタムセルのそれを書く(あなたがこれを行う場合は、ドンtは機能のためにカスタムセルからのデリゲートを呼び出します)、カスタムセル Custom UITableViewCell delegate pattern in Swift

4のためのデリゲートを作成@IBActionを書く)または@IBAction機能

3)へのストーリーボードにあるボタンからの接続を作成する必要がありますビューコントローラに実装する **セルデレゲート=セルフを必要としないでください。デリゲートが呼び出されるとクラッシュします。

例:必要な

CustomCell.swift

protocol CustomCellDelegate { 
    func pressedButton() 
} 

class CustomCell: UITableViewCell { 
    var delegate: CustomCellDelegate! 

    @IBAction func buttonPressed(sender: UIButton) { 
     delegate.pressedButton() 
    } 
} 

ViewController.swift

class CustomClass: UIViewController, CustomCellDelegate { 

    func pressedButton() { 
     // Perform segue here 
    } 
} 
+0

ご返信ありがとうございます。私はステップ1と2を理解していますが、3と4で苦労しています。私は基本的に、ボタンを選択したビューコントローラにセグメンテーションします。 –

+0

@RyanHampton私は答えを更新しました。それが意味をなさないかどうか私に教えてください。 – brl214

関連する問題