在扩展中无法识别 Swift 类属性



这已经绊倒了我一段时间了。我正在尝试将列表取消排队到表视图中(嵌入在视图控制器中)。

class mainViewController: UIViewController {
    var topicsE1: [Topic] = [
        Topic(title: "Practice", startDate: "January 5, 2019"),
        Topic(title: "Fundamentals", startDate: "January 6, 2019")]

扩展名:

extension mainViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "topicCellE1", for: indexPath)
        cell.textLabel?.text = "(topicsE1.title)" //Error message is here
        return cell
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return topicsE1.count
    }
}

该类在另一个文件中定义

class Topic {
    var title: String
    var startDate: String
    init(title: String, startDate: String) {
        self.title = title
        self.startDate = startDate
    }
}

扩展识别了主题E1,但给出了错误

"类型为'[主题]'的值没有成员'标题'"

当代码如上(主题E1.title)时。

你需要

cell.textLabel?.text = topicsE1[indexPath.row].title  

因为topicsE1是一个数组,你不能直接附加.title

最新更新