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

查看:345
本文介绍了Swift 4 func prepare(for segue:UIStoryboardSegue,sender:Any?)不使用indexPath.row更新Int变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

all! I'm trying to combine previous lessons with the Audio Player, but I'm running into an issue. I have a variable activeSong which is equal to indexPath.row using the method func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath). What happens is that when I print  indexPath.row its value is different form activeSong. Any idea why this might be happening. I think I'm doing something wrong with the func prepare(for segue: UIStoryboardSegue, sender: Any?) method. Thanks!!

/
//  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
        }
    }
}


推荐答案

在设置 activeSong 之前,您先调用 performSegue 。交换订单。

You call performSegue before setting activeSong. Exchange the order.

实际上,您不需要 activeSong 。无论如何,当您移交 indexPath 时,只需使用它即可:

Actually you don't need activeSong. As you hand over the indexPath anyway just use it:

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
    }
}

这篇关于Swift 4 func prepare(for segue:UIStoryboardSegue,sender:Any?)不使用indexPath.row更新Int变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆