swift - xcode Playground 中的 UITableView 不显示文本/字幕

标签 swift xcode swift-playground wwdc

我正在关注一篇有关 WWDC17 的文章。一切都很好,但我的实时 View 看起来并不像应有的那样。 (.subtitle) 和表格单元格未显示任何内容

有人可以帮助我如何完成这项工作吗?

以下是 Playground :

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
}

这是固定的 Playground :

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

关于swift - xcode Playground 中的 UITableView 不显示文本/字幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49475025/

相关文章:

ios - Xcode 7, Assets 目录通用设备背景图片支持吗?

swift - 如何在 Swift Playground 中调用 XCTest?

ios - 使用 cocoapods 时如何将项目代码公开到 Xcode Playground ?

ios - Things 3 如何为状态栏文本着色

ios - 无法在 Swift 的 ViewController 类中创建子类的对象

iphone - 在 iPad 上将通用应用程序作为 'legacy' iPhone 应用程序运行

xcode - 向我展示带有 Upsurge Playground 的 Xcode 7.2.1 工作区的工作示例吗?

ios - 数字检测中的 Swift IOS 自定义操作

swift - 使用搜索栏时桌面 View 中的图片会发生变化

ios - 如何避免 IQKeyboard 也向上滚动导航栏?