我正在使用 Swift 5 来使用 XCode 10.2.1 中的 UITableView
。According 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
}
}
但是我的代码无法编译。我得到的只是一个“方法不会覆盖其父类(super class)中的任何方法”错误。什么?我什至使用了 XCode 的自动完成功能,它为我生成了 stub ,但我仍然无法构建我的项目。解决方案是什么?
最佳答案
您的类的父类(super class)是 NSObject
,它没有要在子类中重写的那些 TableView 数据源方法。我相信这就是编译器所说的。
我认为,如果您去掉这些函数声明上的 override
关键字,可能会有所帮助。
关于ios - 无法采用 UITableViewDataSource 协议(protocol) - 方法不会覆盖其父类(super class)中的任何方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57119719/