无法采用UITableViewDataSource协议-方法不会覆盖其超类中的任何方法

时间:2019-07-19 21:22:15

标签: ios xcode uitableview protocols

我正在使用Swift 5在XCode 10.2.1中使用UITableViewAccording to the Apple Developer docs,采用UITableViewDataSource是填充UITableView的最直接方法具有动态数据。

所以我复制了必要的方法以覆盖到自定义类中:

import Foundation
import UIKit

class MyDataSource : NSObject, UITableViewDataSource {
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        // Fetch a cell of the appropriate type.
        let cell = tableView.dequeueReusableCell(withIdentifier: "cellTypeIdentifier", for: indexPath)

        // Configure the cell’s contents.
        cell.textLabel!.text = "Cell text"

        return cell
    }
}

但是我的代码无法编译。我得到的只是一个“方法不会从其超类覆盖任何方法”错误。什么?我什至没有使用过XCode的自动完成功能,它为我生成了存根,但我仍然无法构建项目。解决办法是什么?

enter image description here

1 个答案:

答案 0 :(得分:0)

您的类的超类是NSObject,它没有那些要在子类中覆盖的表视图数据源方法。我相信那是编译器所说的。

我认为,如果您在可能有用的函数声明中取消使用override关键字,那么