在 table 视图中显示名称
Show names on a table view
我需要使用 table 视图加载 thirdviewcontroller
上播放列表的所有名称,但它不起作用。
我试图在第三个视图控制器上显示一个 table 视图,其中包含已创建的所有播放列表名称(我创建了一个包含 class 播放列表元素的数组,本人创作)。在视图加载功能上,我创建了两个播放列表,但是当我尝试使用该应用程序时,名称不会显示在 table 视图中。
我尝试重写代码,link 再次 table 视图并再次创建视图,但它不起作用。它也不会显示任何类型的故障或意外关闭应用程序。
我是 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])
}
}
将 Delegate 和 DataSource 设置为 myTableView2 因此在 viewDidLoad 函数中添加以下两行,然后重新加载 myTableView2。
myTableView2.delegate = self
myTableView2.dataSource = self
您需要将 UITableViews’s
委托和数据源设置为等于自己。
myTableView2.delegate = self;
myTableView2.dataSource = self;
在 viewDidLoad()
super.viewDidLoad().
之后执行此操作
你的数据是什么是你的dataSource,显示什么是你的delegate。您必须设置数据源和委托。您必须设置 tableView dataSource 和 Delegate
myTableView2.delegate = self
myTableView2.dataSource = self
什么是delegate
已从给定的 github 位置下载您的代码。
在我这边效果很好。下图为:
顺便说一句好听的歌!!!
我需要使用 table 视图加载 thirdviewcontroller
上播放列表的所有名称,但它不起作用。
我试图在第三个视图控制器上显示一个 table 视图,其中包含已创建的所有播放列表名称(我创建了一个包含 class 播放列表元素的数组,本人创作)。在视图加载功能上,我创建了两个播放列表,但是当我尝试使用该应用程序时,名称不会显示在 table 视图中。
我尝试重写代码,link 再次 table 视图并再次创建视图,但它不起作用。它也不会显示任何类型的故障或意外关闭应用程序。
我是 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])
}
}
将 Delegate 和 DataSource 设置为 myTableView2 因此在 viewDidLoad 函数中添加以下两行,然后重新加载 myTableView2。
myTableView2.delegate = self
myTableView2.dataSource = self
您需要将 UITableViews’s
委托和数据源设置为等于自己。
myTableView2.delegate = self;
myTableView2.dataSource = self;
在 viewDidLoad()
super.viewDidLoad().
你的数据是什么是你的dataSource,显示什么是你的delegate。您必须设置数据源和委托。您必须设置 tableView dataSource 和 Delegate
myTableView2.delegate = self
myTableView2.dataSource = self
什么是delegate
已从给定的 github 位置下载您的代码。
在我这边效果很好。下图为:
顺便说一句好听的歌!!!