尝试从Class生成简单的tableview。如果我制作单元格=" A"它会在所有单元格中打印出A,但是对于当前的代码输出,它只显示一个空白表格。我究竟做错了什么?对不起自学新手。
import UIKit
class LinesViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var linesTableView: UITableView!
let cellID = "cell"
let lines = ["brown","red","blue"]
class Train {
var color: String = ""
var line: String = ""
init (color:String?, line:String?){
}
}
let brownLine = Train(color: "brown", line: "Brown Line")
let redLine = Train(color: "red", line: "Red Line")
let blueLine = Train(color: "blue", line: "Blue Line")
var trains: [Train] = []
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
linesTableView.delegate = self
linesTableView.dataSource = self
trains = [brownLine,redLine,blueLine]
//print (trains[1].line)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return lines.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = linesTableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath)
let train = trains[indexPath.row]
cell.textLabel?.text = train.line
return cell
}
答案 0 :(得分:1)
您没有将传递值分配给类数据成员。 这就是为什么他们总是空着的。
class Train {
var color: String = ""
var line: String = ""
init (color:String?, line:String?){
self.color = color
self.line = line
}
}