ios - 无法采用 UITableViewDataSource 协议(protocol) - 方法不会覆盖其父类(super class)中的任何方法

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

但是我的代码无法编译。我得到的只是一个“方法不会覆盖其父类(super class)中的任何方法”错误。什么?我什至使用了 XCode 的自动完成功能,它为我生成了 stub ,但我仍然无法构建我的项目。解决方案是什么?

enter image description here

最佳答案

您的类的父类(super class)是 NSObject,它没有要在子类中重写的那些 TableView 数据源方法。我相信这就是编译器所说的。

我认为,如果您去掉这些函数声明上的 override 关键字,可能会有所帮助。

关于ios - 无法采用 UITableViewDataSource 协议(protocol) - 方法不会覆盖其父类(super class)中的任何方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57119719/

相关文章:

ios - 基于 subview 或自定义单元高度的自动布局单元

ios - Webview 总是在 ios Xamarin.Forms 中返回空白屏幕

ios - 在 Swift 中创建线程安全数组

ios - 无法在 cellForRowAtIndexPath 中获取单元格框架或约束

swift - 如何检测是否点击了 TabelCell 内的 UITextView 内的链接,Swift

ios - Xcode 6.1 替换为灰色/不可用

iphone - 下载远程 PLIST iOS - 返回 Null

ios - 标签栏未与UITabBarController一起出现

objective-c - 有一种简单的方法可以在 objective-c 中应用最短路径算法吗?

iOS:修复/锁定纵向模式/方向的相机 View