xcode playground 中的 UITableView 未显示 text/subtitles
UITableView in xcode playground is not showing text/subtitles
我正在关注一篇关于 WWDC17 的文章。一切都很好,但我的实时视图看起来不像它应该的样子。 (.subtitle) 和 table 单元格没有显示任何内容
任何人都可以帮助我如何完成这项工作吗?
操场如下:
import PlaygroundSupport
import UIKit
class WWDCMasterViewController: UITableViewController {
var reasons = ["WWDC is great", "the people are awesome", "I love lab works", "key of success"]
override func viewDidLoad() {
title = "Reason I should attend WWDC18"
view.backgroundColor = .lightGray
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell: UITableViewCell!
cell = tableView.dequeueReusableCell(withIdentifier: "Cell")
if cell == nil {
cell = UITableViewCell(style: .subtitle , reuseIdentifier: "Cell")
cell.accessoryType = .disclosureIndicator
}
let reason = reasons[indexPath.row]
cell.detailTextLabel?.text = "I want to attend because\(reason)"
cell.textLabel?.text = "Reason #\(indexPath.row + 1)"
return cell
}
}
let master = WWDCMasterViewController()
let nav = UINavigationController(rootViewController: master)
PlaygroundPage.current.liveView = nav
由于您缺少 UITableViewDataSource
的方法 numberOfRowsInSection
,因此您的 UITableView 最终只有 0 行,请尝试添加以下内容:
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.reasons.count
}
这里是固定的游乐场:
import PlaygroundSupport
import UIKit
class WWDCMasterViewController: UITableViewController {
var reasons = ["WWDC is great", "the people are awesome", "I love lab works", "key of success"]
override func viewDidLoad() {
title = "Reason I should attend WWDC18"
view.backgroundColor = .lightGray
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.reasons.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell: UITableViewCell! = tableView.dequeueReusableCell(withIdentifier: "Cell")
if cell == nil {
cell = UITableViewCell(style: .subtitle , reuseIdentifier: "Cell")
cell.accessoryType = .disclosureIndicator
}
let reason = reasons[indexPath.row]
cell.detailTextLabel?.text = "I want to attend because\(reason)"
cell.textLabel?.text = "Reason #\(indexPath.row + 1)"
return cell
}
}
let master = WWDCMasterViewController()
let nav = UINavigationController(rootViewController: master)
PlaygroundPage.current.liveView = nav
我正在关注一篇关于 WWDC17 的文章。一切都很好,但我的实时视图看起来不像它应该的样子。 (.subtitle) 和 table 单元格没有显示任何内容
任何人都可以帮助我如何完成这项工作吗?
操场如下:
import PlaygroundSupport
import UIKit
class WWDCMasterViewController: UITableViewController {
var reasons = ["WWDC is great", "the people are awesome", "I love lab works", "key of success"]
override func viewDidLoad() {
title = "Reason I should attend WWDC18"
view.backgroundColor = .lightGray
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell: UITableViewCell!
cell = tableView.dequeueReusableCell(withIdentifier: "Cell")
if cell == nil {
cell = UITableViewCell(style: .subtitle , reuseIdentifier: "Cell")
cell.accessoryType = .disclosureIndicator
}
let reason = reasons[indexPath.row]
cell.detailTextLabel?.text = "I want to attend because\(reason)"
cell.textLabel?.text = "Reason #\(indexPath.row + 1)"
return cell
}
}
let master = WWDCMasterViewController()
let nav = UINavigationController(rootViewController: master)
PlaygroundPage.current.liveView = nav
由于您缺少 UITableViewDataSource
的方法 numberOfRowsInSection
,因此您的 UITableView 最终只有 0 行,请尝试添加以下内容:
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.reasons.count
}
这里是固定的游乐场:
import PlaygroundSupport
import UIKit
class WWDCMasterViewController: UITableViewController {
var reasons = ["WWDC is great", "the people are awesome", "I love lab works", "key of success"]
override func viewDidLoad() {
title = "Reason I should attend WWDC18"
view.backgroundColor = .lightGray
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.reasons.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell: UITableViewCell! = tableView.dequeueReusableCell(withIdentifier: "Cell")
if cell == nil {
cell = UITableViewCell(style: .subtitle , reuseIdentifier: "Cell")
cell.accessoryType = .disclosureIndicator
}
let reason = reasons[indexPath.row]
cell.detailTextLabel?.text = "I want to attend because\(reason)"
cell.textLabel?.text = "Reason #\(indexPath.row + 1)"
return cell
}
}
let master = WWDCMasterViewController()
let nav = UINavigationController(rootViewController: master)
PlaygroundPage.current.liveView = nav