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)
}
}
}
この返信いただきありがとうございます、今これは意味がありますが、私はこのコード行で2番目のビューコントローラで1つの問題があります:var selectedAudio:Audio!宣言されていないタイプを使用する「オーディオ」 – Elfuthark
あなたは別のAudio.swiftファイルにこのコードを配置する必要があります 構造体オーディオ{ するvarタイトル:文字列 するvar imagenameの:文字列 するvar DESCR:文字列} –
素晴らしい仕事が、私は、私は設定していた気づきました私の構造体のすべてが正しくない文字列として、私はこのイメージに変更することによって画像を取得しましたimageName:UIImageしかし、私は何を変更すべきかわからないvar audioURL:これは私のオーディオファイルです。オーディオファイル名の文字列 – Elfuthark