错误:方法未覆盖其父类中的任何方法

时间:2019-11-15 08:05:03

标签: ios swift

我正在以编程方式在ui中创建一个表视图,我遵循的代码没有问题,但是我一直告诉我我有上面的错误?请帮助

错误表明它在此行:override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

这是我的代码的一部分:-

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    return tableView.dequeueReusableCell(withIdentifier: "cellId", for: indexPath as IndexPath)
}

class MyCell: UITableViewCell {

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupViews()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func setupViews() {
    }
}

3 个答案:

答案 0 :(得分:0)

不确定,但似乎您在UIViewController中有tableView吗?如果这样,则在 func cellForRowAtIndexPath 之前不需要覆盖。只需删除替代文字。

答案 1 :(得分:0)

如果您使用UIViewController,只需添加UITableViewDataSourceUITableViewDelegate方法,如下例所示,并且不包括override关键字:

class MyViewController: UIViewController , UITableViewDataSource, UITableViewDelegate{

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


    }

    //Other UITableViewDataSource and UITableViewDelegate methods goes here
}

如果使用UITableViewController,则可以覆盖UITableViewDataSourceUITableViewDelegate方法,如下例所示:

class MyTableViewController: UITableViewController{

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


    }

    //override other UITableViewDataSource, UITableViewDelegate methods here
}

希望有帮助!

答案 2 :(得分:0)

cellForRowAtIndexPath是Swift 2代码。请寻找提供现代代码的教程。

Swift 3+的语法是

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

仅当类是override的子类时,才需要UITableViewController关键字