Swift 4 func prepare(for segue: UIStoryboardSegue, sender: Any?) 不使用 indexPath.row 更新 Int 变量
Swift 4 func prepare(for segue: UIStoryboardSegue, sender: Any?) not updating Int variable with indexPath.row
全部!我正在尝试将以前的课程与音频播放器结合起来,但我 运行 遇到了问题。我有一个变量 activeSong
,它使用方法 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
等于 indexPath.row
。发生的事情是,当我打印 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
}
}
}
改变performSegue(withIdentifier: "toNowPlaying", sender: indexPath)
到performSegue(withIdentifier: "toNowPlaying", sender: self)
您在设置 activeSong
之前调用 performSegue
。交换订单。
其实你不需要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
}
}
全部!我正在尝试将以前的课程与音频播放器结合起来,但我 运行 遇到了问题。我有一个变量 activeSong
,它使用方法 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
等于 indexPath.row
。发生的事情是,当我打印 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
}
}
}
改变performSegue(withIdentifier: "toNowPlaying", sender: indexPath)
到performSegue(withIdentifier: "toNowPlaying", sender: self)
您在设置 activeSong
之前调用 performSegue
。交换订单。
其实你不需要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
}
}