質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.51%
Swift 2

Swift 2は、Apple社が独自に開発を行っている言語「Swift」のアップグレード版です。iOSやOS X、さらにLinuxにも対応可能です。また、throws-catchベースのエラーハンドリングが追加されています。

Q&A

解決済

1回答

1853閲覧

プロジェクトに追加した音を鳴らしたいのですが、エラーが出てしまいます

surf

総合スコア17

Swift 2

Swift 2は、Apple社が独自に開発を行っている言語「Swift」のアップグレード版です。iOSやOS X、さらにLinuxにも対応可能です。また、throws-catchベースのエラーハンドリングが追加されています。

0グッド

0クリップ

投稿2016/09/01 14:01

プロジェクトに4曲追加して、それをセルを選択することで鳴るようにしたいのですが、『Value of type "ViewController" has no member 'mytableview'』というエラーが出てしまいます。

<ViewControllerのコード>
import UIKit
import AVFoundation

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

let imageNames = ["futako.jpg", "yokado.jpg", "fran.jpg", "zikken.jpg"] let imageTitles = ["イヌ2", "ネコ2", "イヌ1", "イヌ2"] let imageDescriptions = [ "イヌ", "ネコ", "イヌ", "イヌ" ] var audioPlayer = AVAudioPlayer() override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return imageNames.count } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("MyCell") as! CustomTableViewCell cell.setCell(imageNames[indexPath.row], titleText: imageTitles[indexPath.row], descriptionText: imageDescriptions[indexPath.row]) return cell } @IBAction func tapButton(sender: AnyObject) { let btn = sender as! UIButton let cell = btn.superview?.superview as! UITableViewCell let row = self.mytableview.indexPathForCell(cell)?.row //(この上のコードの箇所でエラーが発生します) let fileName = "sound\(row! + 1)" print(fileName) //let soundPath = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(fileName, ofType: "mp3")!)

}

}

<CustomTableViewCellのコード>
import UIKit

class CustomTableViewCell: UITableViewCell {

@IBOutlet weak var myImageView: UIImageView! @IBOutlet weak var myTitleLabel: UILabel! @IBOutlet weak var myDescriptionLabel: UILabel! @IBOutlet weak var tapButton: UIButton! override func awakeFromNib() { super.awakeFromNib() // Initialization code } override func setSelected(selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) // Configure the view for the selected state } func setCell(imageName: String, titleText: String, descriptionText: String) { myImageView.image = UIImage(named: imageName) myTitleLabel.text = titleText myDescriptionLabel.text = descriptionText }

}

どこをどう直せばエラーが消え、音がなるか困っています。
よろしくお願い致します。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

セルのボタンアクションをセルに結んでください。

delegateを使用してViewControllerの処理を呼び出しています、cell生成時にセルにindexを渡してDelegateメソッドを呼び出す時の引数に更に渡しています。

swift

1import UIKit 2import AVFoundation 3 4class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, CustomTableViewCellDelegate { 5 6 let imageNames = ["futako.jpg", "yokado.jpg", "fran.jpg", "zikken.jpg"] 7 8 let imageTitles = ["イヌ2", "ネコ2", "イヌ1", "イヌ2"] 9 var audioPlayer = AVAudioPlayer() 10 11 let imageDescriptions = [ 12 "イヌ", 13 "ネコ", 14 "イヌ", 15 "イヌ" 16 ] 17 18 override func viewDidLoad() { 19 super.viewDidLoad() 20 // Do any additional setup after loading the view, typically from a nib. 21 } 22 23 override func didReceiveMemoryWarning() { 24 super.didReceiveMemoryWarning() 25 // Dispose of any resources that can be recreated. 26 } 27 28 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 29 return imageNames.count 30 } 31 32 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 33 34 let cell = tableView.dequeueReusableCellWithIdentifier("MyCell") as! CustomTableViewCell 35 36 cell.setCell(imageNames[indexPath.row], titleText: imageTitles[indexPath.row], descriptionText: imageDescriptions[indexPath.row]) 37 cell.index = indexPath 38 cell.delegate = self 39 40 return cell 41 } 42 43 func selectCellButton(index: NSIndexPath) { 44 let fileName = "sound\(index.row + 1)" 45 46 print(fileName) 47 48 do { 49 let filePath = NSBundle.mainBundle().pathForResource(fileName, ofType: "mp3") 50 let audioPath = NSURL(fileURLWithPath: filePath!) 51 audioPlayer = try AVAudioPlayer(contentsOfURL: audioPath) 52 if audioPlayer.prepareToPlay() { 53 audioPlayer.play() 54 } 55 } catch { 56 print("Error") 57 } 58 } 59} 60 61// <CustomTableViewCellのコード> 62import UIKit 63 64// protocol定義 65protocol CustomTableViewCellDelegate: class { 66 func selectCellButton(index: NSIndexPath) 67} 68 69class CustomTableViewCell: UITableViewCell { 70 71 @IBOutlet weak var myImageView: UIImageView! 72 @IBOutlet weak var myTitleLabel: UILabel! 73 @IBOutlet weak var myDescriptionLabel: UILabel! 74 @IBOutlet weak var tapButton: UIButton! 75 76 weak var delegate: CustomTableViewCellDelegate! 77 var index: NSIndexPath! 78 79 override func awakeFromNib() { 80 super.awakeFromNib() 81 // Initialization code 82 } 83 84 override func setSelected(selected: Bool, animated: Bool) { 85 super.setSelected(selected, animated: animated) 86 87 // Configure the view for the selected state 88 } 89 90 func setCell(imageName: String, titleText: String, descriptionText: String) { 91 myImageView.image = UIImage(named: imageName) 92 myTitleLabel.text = titleText 93 myDescriptionLabel.text = descriptionText 94 } 95 96 @IBAction func tapButton(sender: AnyObject) { 97 delegate?.selectCellButton(index) 98 } 99}

投稿2016/09/01 22:46

編集2016/09/01 23:21
_Kentarou

総合スコア8490

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

surf

2016/09/02 11:55

cell.delegate = selfの箇所で Cannot assign value of type 'ViewController' to type 'CustomTableViewCellDelegate!'というエラーが表示されます。
_Kentarou

2016/09/02 12:16 編集

CustomTableViewCellDelegateを継承するのを忘れているのではないですか? class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, CustomTableViewCellDelegate { もしくはfunc selectCellButton(index: NSIndexPath) メソッドが抜けていませんか?
surf

2016/09/03 11:32

ありがとうございます。 エラーは出ないようになったのですが・・・もう一つの質問同様、 シュミレーターや実機で実行すると画面が真っ白になってしまいます。 エラーが出ない以上、答えようがないかもしれませんが、 何か考えられることがあれば、教えてください。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.51%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問