Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/109.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在表视图中显示信息_Ios_Swift_Uitableview_Uiviewcontroller - Fatal编程技术网

Ios 如何在表视图中显示信息

Ios 如何在表视图中显示信息,ios,swift,uitableview,uiviewcontroller,Ios,Swift,Uitableview,Uiviewcontroller,我试图在表视图中加载字符串数组,但该视图无法识别该数组 我有一个名为Playlists(在ThirdViewController上声明为全局)的数组,其中包含类Playlist中的对象。当我在其他每个表视图上使用它时,我可以访问每个对象并在表视图上使用它(我在ThirdViewController上使用它),但在AddToPlaylist视图上我不能使用它。我认为我正确地将单元格和func用于表视图 当我按下播放器视图上的“Añadir”按钮时,就会发生这种情况。它应该用数组信息加载表视图 这是

我试图在表视图中加载字符串数组,但该视图无法识别该数组

我有一个名为Playlists(在ThirdViewController上声明为全局)的数组,其中包含类Playlist中的对象。当我在其他每个表视图上使用它时,我可以访问每个对象并在表视图上使用它(我在ThirdViewController上使用它),但在AddToPlaylist视图上我不能使用它。我认为我正确地将单元格和func用于表视图

当我按下播放器视图上的“Añadir”按钮时,就会发生这种情况。它应该用数组信息加载表视图

这是项目(开发分支):

以下是播放列表阵列的声明:


import UIKit
import AVFoundation


var favorites:[String] = []
var Playlists:[Playlist] = []
var selecPlaylist = 0
var firstOpen2 = true

class ThirdViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var myTableView2: UITableView!

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        //print(Playlists.count)
        return Playlists.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
        cell.textLabel?.text = Playlists[indexPath.row].name

        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        selecPlaylist = indexPath.row
        performSegue(withIdentifier: "segue", sender: self)
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        myTableView2.delegate = self
        myTableView2.dataSource = self

        if firstOpen2{
            crear()
            firstOpen2 = false
        }

        myTableView2.reloadData()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func crear(){
        let pl1 = Playlist(name: "Prueba")

        pl1?.addSong(song: songs[0])
        Playlists.append(pl1!)

        let pl2 = Playlist(name: "Prueba2")

        pl2?.addSong(song: songs[1])
        Playlists.append(pl2!)
    }


}


您的
numberOfRowsInSection
是否被呼叫?如果是,它会返回什么值?是的,会调用numberOfRowsInSection并返回播放列表。count。您的tableview是如何创建的?我在这里看不到它,而且您的项目不包含一个值,
Playlists.count当时的值是…?Playlists.count应该是2,但如果我打印它,它会显示0。我在这个项目的另一个表视图中也是这样做的。
tableView.dequeueReusableCell
已经返回了
UITableViewCell
。强制强制转换为它已经返回的类型是没有意义的。这个答案毫无帮助。在发布答案时,请解释错误以及答案如何解决问题。

import UIKit
import AVFoundation


var favorites:[String] = []
var Playlists:[Playlist] = []
var selecPlaylist = 0
var firstOpen2 = true

class ThirdViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var myTableView2: UITableView!

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        //print(Playlists.count)
        return Playlists.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
        cell.textLabel?.text = Playlists[indexPath.row].name

        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        selecPlaylist = indexPath.row
        performSegue(withIdentifier: "segue", sender: self)
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        myTableView2.delegate = self
        myTableView2.dataSource = self

        if firstOpen2{
            crear()
            firstOpen2 = false
        }

        myTableView2.reloadData()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func crear(){
        let pl1 = Playlist(name: "Prueba")

        pl1?.addSong(song: songs[0])
        Playlists.append(pl1!)

        let pl2 = Playlist(name: "Prueba2")

        pl2?.addSong(song: songs[1])
        Playlists.append(pl2!)
    }


}

let cell = tableView.dequeueReusableCell(withIdentifier: "yourIdentifier") as! UITableViewCell

cell.textLabel?.text = Playlists[indexPath.row].name
return cell