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

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

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

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

Q&A

解決済

1回答

1952閲覧

table cellでは一つのボタンで再生と停止というのはできないのでしょうか?

surf

総合スコア17

Swift 2

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

0グッド

0クリップ

投稿2016/09/06 12:21

編集2016/09/08 10:55

セルごとに異なる曲を再生したく、プロジェクトに4曲(sound01〜sound04)を保存して、以下のコードを書きました。
そして『Play』ボタンを押すと再生して『Pause』と表示され、『Pause』を押すとボタンが『Play』に変わるようにしたいのですが、以下のエラーが出ます。

エラー内容①:
The btnPlayPause outlet from the SecondViewController to the UIButton is invalid. Outlets cannot be connected to repeating content.

エラー内容②:
Use of unresolved identifier 'audioPath'

//ViewControllerのコード import UIKit import AVFoundation class SecondViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, CustomTableViewCellDelegate { var player = AVAudioPlayer() let imageNames = ["futako.jpg", "yokado.jpg", "fran.jpg", "zikken.jpg"] let imageTitles = ["イヌ2", "ネコ2", "イヌ1", "イヌ2"] var audioPlayer = AVAudioPlayer() let imageDescriptions = [ "イヌ", "ネコ", "イヌ", "イヌ" ] override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. player = AVAudioPlayer(contentsOfURL: audioPath, error: nil) player.prepareToPlay() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } @IBOutlet weak var btnPlayPause: UIButton! @IBAction func btnPlayPause(sender: UIButton) { if (player.playing) { player.pause() btnPlayPause.setTitle("Play", forState: UIControlState.Normal) } else { player.play() btnPlayPause.setTitle("Pause", forState: UIControlState.Normal) } } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return imageNames.count } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("MyCell1") as! CustomTableViewCell1 cell.setCell(imageNames[indexPath.row], titleText: imageTitles[indexPath.row], descriptionText: imageDescriptions[indexPath.row]) cell.index = indexPath cell.delegate = self return cell } func selectCellButton(index: NSIndexPath) { let fileName = "sound\(index.row + 1)" print(fileName) do { let filePath = NSBundle.mainBundle().pathForResource(fileName, ofType: "mp3") let audioPath = NSURL(fileURLWithPath: filePath!) audioPlayer = try AVAudioPlayer(contentsOfURL: audioPath) if audioPlayer.prepareToPlay() { audioPlayer.play() } } catch { print("Error") } } } //CustomTableViewCellのコード import UIKit protocol CustomTableViewCellDelegate: class { func selectCellButton(index: NSIndexPath) } class CustomTableViewCell1: UITableViewCell { @IBOutlet weak var myImageView: UIImageView! @IBOutlet weak var myTitleLabel: UILabel! @IBOutlet weak var myDescriptionLabel: UILabel! @IBOutlet weak var btnPlayPause: UIButton! weak var delegate: CustomTableViewCellDelegate! var index: NSIndexPath! 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

ベストアンサー

UIButtonは、引数で渡されるもの(sender)を使います。

func btnPlayPause()内のbtnPlayPausesenderに変更して下さい。
btnPlayPauseのOutletは必要ないので削除して下さい)

投稿2016/09/09 02:02

fuzzball

総合スコア16731

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問