2017-05-07 1 views
0

私はオーディオタイトルをリストしたテーブルを持っています。タイトルをクリックして、オーディオタイトル、画像、説明、再生ボタンを表示する2番目のView Controllerに移動します。テーブルセルをクリックして、複数の配列から2番目のビューコントローラにデータを送信します。

関連するすべてのデータを保持する4つの配列が作成されていますが、2つ目のView Controllerのラベル、imageViewおよびボタンを取得できないため、テーブル上でクリックされた行が変更されます。ここで

は、最初のビューコントローラのための私のコードのサンプルです:

let audioTitile = ["Bubbling Pools", "March Of Faith"] 

let audioImage = ["1.png", "2.png"] 

let desc = ["The stench of life fills the air. Viscous fluid bubbles to the surface in great exhortations of gas and moisture. Something about these pools is familiar..", 
      "The devoted stream into the distance. The dust from their sandaled feet blocks out the sun for miles around. There's no stopping them. They believe."] 

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

     return audioTitle.count 
    } 

    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

     let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "Cell") 

     cell.textLabel?.text = audioTitle[indexPath.item] 

     return cell 
    } 

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
     performSegue(withIdentifier: "segue", sender: audioTitle[indexPath.row]) 
    } 

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
     let temp = segue.destination as! SecondViewController 

     temp.titleLabel.text = audioTitle[0] // need to replace [0] with ? 
     // temp.artworkImage.image = audioImage[0] This Code is wrong 
     temp.descriptionLabel.text = desc[0] // need to replace [0] with ? 


    } 

    @IBOutlet weak var tableView: UITableView! 

    override var preferredStatusBarStyle: UIStatusBarStyle { 
     return .lightContent 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 

    } 
} 

私の2番目のビューコントローラコード:

import UIKit 
import AVFoundation 

class SecondViewController: UIViewController { 

    @IBOutlet weak var titleLabel: UILabel! 
    @IBOutlet weak var artworkImage: UIImageView! 
    @IBOutlet weak var descriptionLabel: UILabel! 
    @IBOutlet weak var playButton: UIButton! 

    override var preferredStatusBarStyle: UIStatusBarStyle { 
     return .lightContent 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view. 
    } 

    @IBAction func playButtonPressed(_ sender: Any) { 

     playAudio() 

    } 

    var player: AVAudioPlayer? 

    func playAudio() { 
     let url = Bundle.main.url(forResource: "2_Bubbling_Pools.mp3", withExtension: "mp3")! 

     do { 
      player = try AVAudioPlayer(contentsOf: url) 
      guard let player = player else { return } 

      player.prepareToPlay() 
      player.play() 
     } catch let error { 
      print(error.localizedDescription) 
     } 
    } 

} 

答えて

0

あなたはいくつかの方法で配列をパックする必要があります。いくつかの方法で配列をトランジットすることができますが、最も正しい方法は、構造体またはクラスを作成することです。このように構造体を追加します。

struct Audio { 
var title: String 
var imageName: String 
var descr: String } 

そして、最初のViewControllerで、配列を初期化:

let audios = [Audio(title: "Bubbling Pools", imageName: "1.png", descr: "The stench of life fills the air. Viscous fluid bubbles "), 
      Audio(title: "March Of Faith", imageName: "2.png", descr: "The stench of life fills the air. Viscous fluid bubbles ")] 

次に、このようなあなたのFirstVCを変更:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return audios.count 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    let cell = UITableViewCell() 
    let audio = audios[indexPath.row] 
    cell.textLabel?.text = audio.title 
    return cell 
} 

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    let audio = audios[indexPath.row] 

    performSegue(withIdentifier: "segue", sender: audio) 

} 

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    if segue.identifier == "segue" { 

     let audio = sender as! Audio 
     let destinationVC = segue.destination as! SeconViewController 
     destinationVC.selectedAudio = audio 
    } 
} 

そして、このようなあなたのSecondVCを変更します。

class SecondViewController: UIViewController { 

@IBOutlet weak var titleLabel: UILabel! 
@IBOutlet weak var artworkImage: UIImageView! 
@IBOutlet weak var descriptionLabel: UILabel! 
@IBOutlet weak var playButton: UIButton! 

var selectedAudio: Audio! 

override var preferredStatusBarStyle: UIStatusBarStyle { 
    return .lightContent 
} 

override func viewDidLoad() { 
    super.viewDidLoad() 


    // CONFIGURE LABELS HERE USING selectedAudio 

} 

}

+0

この返信いただきありがとうございます、今これは意味がありますが、私はこのコード行で2番目のビューコントローラで1つの問題があります:var selectedAudio:Audio!宣言されていないタイプを使用する「オーディオ」 – Elfuthark

+0

あなたは別のAudio.swiftファイルにこのコードを配置する必要があります 構造体オーディオ{ するvarタイトル:文字列 するvar imagenameの:文字列 するvar DESCR:文字列} –

+0

素晴らしい仕事が、私は、私は設定していた気づきました私の構造体のすべてが正しくない文字列として、私はこのイメージに変更することによって画像を取得しましたimageName:UIImageしかし、私は何を変更すべきかわからないvar audioURL:これは私のオーディオファイルです。オーディオファイル名の文字列 – Elfuthark

関連する問題