【问题标题】:Swift Class Property Not Recognized in Extension扩展中无法识别 Swift 类属性
【发布时间】:2019-06-01 16:55:43
【问题描述】:

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

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
    }
}

扩展识别topicE1,但报错

“'[Topic]'类型的值没有成员'title'”

当代码如上时(topicsE1.title)。

【问题讨论】:

    标签: ios swift uitableview class methods


    【解决方案1】:

    你需要

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

    因为topicsE1 是一个数组,您不能将.title 直接附加到它上面

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多