プロジェクトにsound01〜sound04までの4曲を追加し、セルにButtonを設置してそれを押すと音が鳴るように以下のコードを書いたのですが、画面が真っ白になってしまいます。また、停止ボタンを追加するとエラーが出てしまい、画面が白くなるのと関係があるのかと思い、今はその部分のコードを書いてませんが、それも解決できれば解決したいです。
//<ViewControllerのコード>
import UIKit
import AVFoundation
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, CustomTableViewCellDelegate {
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. } 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]) 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定義
protocol CustomTableViewCellDelegate: class {
func selectCellButton(index: NSIndexPath)
}
class CustomTableViewCell: UITableViewCell {
@IBOutlet weak var myImageView: UIImageView! @IBOutlet weak var myTitleLabel: UILabel! @IBOutlet weak var myDescriptionLabel: UILabel! @IBOutlet weak var tapButton: 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 } @IBAction func tapButton(sender: AnyObject) { delegate?.selectCellButton(index) }
}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/09/10 00:32
退会済みユーザー
2016/09/10 01:03
退会済みユーザー
2016/09/10 01:11
2016/09/10 04:15