UITableView на игровой площадке xcode не отображает текст/субтитры

Я следил за статьей о WWDC17. Все было хорошо, но мой вид в реальном времени выглядит не так, как должно быть. (.subtitle) и ячейка таблицы ничего не показывают

Может ли кто-нибудь помочь мне, как я могу выполнить эту работу?

Далее детская площадка:

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

person singlepiece000    schedule 25.03.2018    source источник


Ответы (1)


Поскольку вам не хватает метода 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
person AamirR    schedule 25.03.2018