Swift 4 func prepare(for segue:UIStoryboardSegue,sender:Any?)不使用indexPath.row更新Int变量

时间:2018-11-01 16:08:40

标签: arrays swift segue uistoryboardsegue

全部!我试图将以前的课程与音频播放器结合起来,但是遇到了一个问题。我有一个activeSong变量,它使用方法indexPath.row等于func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)。发生的是,当我打印indexPath.row时,其值与activeSong的值不同。知道为什么会这样。我认为func prepare(for segue: UIStoryboardSegue, sender: Any?)方法做错了。谢谢!

/
//  MySongsController.swift
//  Audio Player
//
//  Created by Alex Gomez on 10/31/18.
//  Copyright © 2018 Alex Gomez. All rights reserved.
//

import UIKit

var activeSong = -1
var isPlaying = false

class MySongsController: UITableViewController {

    var songsList = [Song()]

    @IBOutlet var table: UITableView!


    override func viewDidLoad() {
        super.viewDidLoad()

        if songsList.count == 1 && songsList[0].songTitle == "" {
            songsList.remove(at: 0)
            songsList.append(Song(songTitle: "El Colibri", songArtist: "Santiago Feliu", songFormat: "MP3"))
        }

        songsList.append(Song(songTitle: "Amargas Verdades", songArtist: "Santiago Feliu", songFormat: "MP3"))
        table.reloadData()

        // print("There are \(songsList.count) songs in the list")
        // print("Active song is \(activeSong)")
    }


    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    // Number of cells
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return songsList.count
    }

    // Cell title
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

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

        cell.textLabel?.text = "\(songsList[indexPath.row].songTitle) — \(songsList[indexPath.row].songArtist)"

        return cell
    }

    // Go to "Now Playing" view once a cell is selected
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        performSegue(withIdentifier: "toNowPlaying", sender: indexPath)
        activeSong = indexPath.row

        // print("—––––")
        // print("Index Path row is \(indexPath.row)")
        // print("Active song \(activeSong) — \(songsList[activeSong].songTitle)")

    }

    // Pass song information along to Now Playing view
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "toNowPlaying" {
            let nowPlayingView = segue.destination as! ViewController
            nowPlayingView.playing = activeSong
        }
    }
}

2 个答案:

答案 0 :(得分:0)

更改app.component.ts

performSegue(withIdentifier: "toNowPlaying", sender: indexPath)

答案 1 :(得分:0)

您在设置performSegue之前先致电activeSong。交换订单。

实际上,您不需要activeSong。无论如何,您在移交indexPath时都可以使用它:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "toNowPlaying" {
        let nowPlayingView = segue.destination as! ViewController
        let indexPath = sender as! IndexPath
        nowPlayingView.playing = indexPath.row
    }
}