在 iPad 版 Swift Playgrounds 中显示 TableView
Posted
技术标签:
【中文标题】在 iPad 版 Swift Playgrounds 中显示 TableView【英文标题】:Show TableView in Swift Playgrounds for iPad 【发布时间】:2016-09-25 02:15:02 【问题描述】:除了我尝试在实时视图中显示tableView
的最后一行不起作用:PlaygroundPage.current.liveView = controller
我不知道我做错了什么?
import UIKit
import PlaygroundSupport
import Foundation
class TableViewController: UITableViewController
let tableData = ["Matthew", "Mark", "Luke", "John"]
override func viewDidLoad()
super.viewDidLoad()
print("Hello Matt")
override func numberOfSections(in tableView: UITableView) -> Int
return 1
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
return tableData.count
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
var cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as UITableViewCell
cell.textLabel?.text = tableData[indexPath.row]
return cell
let controller = TableViewController()
PlaygroundPage.current.liveView = controller
【问题讨论】:
什么不起作用?有错误吗?会发生什么? @JackLawrence akosma 搞定了,感谢您调查并检查问题所在 gist.github.com/watert/13c38d269ea15aa8360f 【参考方案1】:我想你忘了在viewDidLoad()
中注册表格视图单元格
override func viewDidLoad()
super.viewDidLoad()
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
【讨论】:
【参考方案2】:在func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
中,在函数顶部添加一行:
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
像这样:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
var cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as UITableViewCell
cell.textLabel?.text = tableData[indexPath.row]
return cell
【讨论】:
以上是关于在 iPad 版 Swift Playgrounds 中显示 TableView的主要内容,如果未能解决你的问题,请参考以下文章