Ios 通过单击表格单元格将数据从多个阵列发送到第二个视图控制器

Ios 通过单击表格单元格将数据从多个阵列发送到第二个视图控制器,ios,arrays,uitableview,viewcontroller,Ios,Arrays,Uitableview,Viewcontroller,我有一个列出音频标题的表格。我想点击一个标题,它会把我带到第二个视图控制器,该控制器显示音频标题、图片、描述和播放按钮 我创建了4个数组,每个数组保存所有相关数据,但我无法将第二个视图控制器上的标签、imageView和按钮更改为表上单击的任何行 以下是我的第一个视图控制器代码示例: let audioTitile = ["Bubbling Pools", "March Of Faith"] let audioImage = ["1.png", "2.png"] let desc = ["T

我有一个列出音频标题的表格。我想点击一个标题,它会把我带到第二个视图控制器,该控制器显示音频标题、图片、描述和播放按钮

我创建了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)
        }
    }

}

您应该以某种方式打包阵列。可以用几种方法传输数组,但最正确的方法是创建结构或类。添加如下结构:

struct Audio {
var title: String
var imageName: String
var descr: String }
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
    }
}
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

}
然后在第一个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 ")]
然后像这样更改您的第一个VC:

struct Audio {
var title: String
var imageName: String
var descr: String }
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
    }
}
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

}
然后像这样改变你的第二个VC:

struct Audio {
var title: String
var imageName: String
var descr: String }
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
    }
}
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

}

}

非常感谢您的回复,现在这很有意义,但我的第二个视图控制器在这行代码中确实存在一个问题:var selectedAudio:Audio!使用未声明的类型“Audio”您应该将此代码放在单独的Audio.swift文件中:struct Audio{var title:String var imageName:String var descr:String}效果很好,但我注意到我将struct中的所有内容都设置为字符串,这是不正确的,我通过更改为这个var-imageName:UIImage使我的图像正常工作,但不确定我应该将var-audioURL:String更改为什么,这意味着这是我的音频文件,我这里只有一个音频文件名字符串,您可以使用音频结构中的任何类型。图像、字符串等等。我只是建议使用图像名称的字符串数组,因为这正是您在示例中使用let audioImage=[“1.png”,“2.png”]