2017-05-07 53 views
0

我有一个列出音频标题的表格。我想点击一个标题,它会带我到第二个视图控制器,显示音频标题,图片,说明和播放按钮。通过单击表格单元格将数据从多个阵列发送到第二个视图控制器

我有4个数组创建每个持有所有相关的数据,但我无法获得第二个视图控制器上的标签,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. 

    } 
} 

我的第二个视图控制器代码:

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

您应该包以某种方式排列。您可以通过多种方式传输数组,但最正确的方法是创建结构体或类。这样添加的结构:在第一的ViewController

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

然后,初始化数组:

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

非常感谢这个回复,现在有道理,但我在第二个视图控制器中有这样一行代码:var selectedAudio:Audio!使用未声明的类型“音频” – Elfuthark

+0

,你应该将此代码放在单独的文件Audio.swift: 结构音频{VAR 标题:字符串 VAR imageName:字符串 VAR DESCR:字符串}是伟大的工作,但我发现我已经建立 –

+0

一切在我的结构作为字符串这是不正确的,我得到我的图像工作,通过改变这个var imageName:UIImage但不知道我应该改变var audioURL:字符串到这是为了我的音频文件,我在这里有一个我的音频文件名称的字符串 – Elfuthark

相关问题