【问题标题】:How to show info on table view如何在表格视图上显示信息
【发布时间】:2019-09-21 04:17:57
【问题描述】:

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

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

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

这里是项目(开发分支): tree/develop

import UIKit

class AddToPlaylist: UIViewController, UITableViewDelegate, UITableViewDataSource{

    @IBOutlet weak var myTableViewPlaylist: UITableView!
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return Playlists.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "hola", for: indexPath)

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

        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        Playlists[indexPath.row].songs.append(songName)
        performSegue(withIdentifier: "addedSong", sender: self)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        myTableViewPlaylist.delegate = self
        myTableViewPlaylist.dataSource = self

        myTableViewPlaylist.reloadData()
    }
}

下面是 Playlists 数组的声明:


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 被调用并且我返回 Playlists.count。
  • 你的tableview是如何创建的?我在这里看不到它,您的项目不包含 xib
  • Playlists.count当时的值是……?
  • Playlists.count 应该是 2,但如果我打印它会显示 0。我在这个项目的另一个表视图中也是这样做的。

标签: ios swift uitableview uiviewcontroller


【解决方案1】:
let cell = tableView.dequeueReusableCell(withIdentifier: "yourIdentifier") as! UITableViewCell

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

【讨论】:

  • tableView.dequeueReusableCell 已经返回 UITableViewCell。强制转换为它已经返回的类型是没有意义的。这个答案没有任何帮助。发布答案时,请说明问题所在以及答案如何解决问题。
猜你喜欢
  • 1970-01-01
  • 2020-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-14
相关资源
最近更新 更多