【问题标题】:Show names on a table view在表格视图上显示名称
【发布时间】:2019-09-19 21:53:46
【问题描述】:

我需要使用表格视图加载thirdviewcontroller 上所有播放列表的名称,但它不起作用。

我试图在第三个视图控制器上显示一个表格视图,其中包含已创建的播放列表的所有名称(我创建了一个包含我自己创建的类播放列表元素的数组)。在视图上确实加载了函数,我创建了两个播放列表,但是当我尝试应用程序时,名称不会显示在表格视图中。

我曾尝试重写代码,再次链接表格视图并再次创建视图,但它不起作用。它也不会显示任何类型的故障或意外关闭应用程序。

我是 Swift 新手,所以我不知道我是否会做更多错误。

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

//
//  ThirdViewController.swift
//  reproductor
//
//  Created by Macosx on 24/4/19.
//  Copyright © 2019 mamechapa. All rights reserved.
//

import UIKit
import AVFoundation


var favorites:[String] = []
var Playlists:[Playlist] = []

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) {
        //
    }
    override func viewDidLoad() {
        print("viewdidload")
        super.viewDidLoad()

        crear()

        myTableView2.reloadData()
        print(Playlists[1].name)
    }

    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!)
        print(Playlists[0].name)
        print(Playlists[0].songs[0])

        let pl2 = Playlist(name: "Prueba2")

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


}

【问题讨论】:

    标签: ios swift uitableview uiviewcontroller


    【解决方案1】:

    你的数据是什么就是你的数据源,显示什么是你的委托。您必须设置数据源和委托。你必须设置tableView dataSource 和 Delegate

    myTableView2.delegate = self
    myTableView2.dataSource = self
    
    

    什么是delegate

    UITableViewDelegate

    UITableViewDataSource

    【讨论】:

      【解决方案2】:

      将 Delegate 和 DataSource 设置为 myTableView2 因此在 viewDidLoad 函数中添加以下两行,然后重新加载 myTableView2。

      myTableView2.delegate = self
      myTableView2.dataSource = self
      

      【讨论】:

        【解决方案3】:

        从给定的 github 位置下载了您的代码。

        它在我的最后工作正常。下图是:

        顺便说一句,好听的歌!!!

        【讨论】:

          【解决方案4】:

          你需要设置UITableViews’s委托和数据源等于self。

          myTableView2.delegate = self;
          myTableView2.dataSource = self;
          

          viewDidLoad()super.viewDidLoad(). 之后执行此操作

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2012-09-05
            • 1970-01-01
            • 2021-07-10
            • 1970-01-01
            • 2011-06-05
            • 1970-01-01
            • 2016-06-21
            • 2019-09-21
            相关资源
            最近更新 更多