'override'只能在类成员上指定 - Xcode 9.0,Swift 4

时间:2017-11-16 23:49:58

标签: swift uitableview override viewdidload

我一直收到关于覆盖功能的错误消息“'覆盖'只能在类成员上指定”。我尝试在覆盖功能之前插入一个“}”,但收到了更多的错误。

    import UIKit

    class MainVC: UIViewController, UITableViewDelegate, UITableViewDataSource {


    @IBOutlet weak var tableVideo: UITableView!

        var partyRocks = [PartyRock]()


        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return partyRocks.count
}

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


            tableView.delegate = self
            tableView.dataSource = self

            if let cell = tableView.dequeueReusableCell(withIdentifier: "PartyCell", for: indexPath) as? PartyCell {

                let partyRock = partyRocks[indexPath.row]


                return cell
            } else {



            return UITableViewCell()
        }


           override func viewDidLoad() {
            super.viewDidLoad()


        }
        let p1 = PartyRock(imageURL: "https://www.lonelyplanet.com/travel-tips-and-articles/eight-amazing-cities-for-street-art/40625c8c-8a11-5710-a052-1479d276ed25", videoURL: "<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/9f31OCHXAsI\" frameborder=\"0\" allowfullscreen></iframe>", videoTitle: "Stephen's Artwork")


        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return partyRocks.count
        }

        }

    }

1 个答案:

答案 0 :(得分:0)

这可以摆脱覆盖错误 - 一些奇怪的大括号用法可能是由于你的tableview方法的剪切和粘贴不准确造成的......

import UIKit

class MainVC: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var tableVideo: UITableView!

    var partyRocks = [PartyRock]()

    let p1 = PartyRock(imageURL: "https://www.lonelyplanet.com/travel-tips-and-articles/eight-amazing-cities-for-street-art/40625c8c-8a11-5710-a052-1479d276ed25", videoURL: "<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/9f31OCHXAsI\" frameborder=\"0\" allowfullscreen></iframe>", videoTitle: "Stephen's Artwork")

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return partyRocks.count
    }

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

        tableView.delegate = self
        tableView.dataSource = self

        if let cell = tableView.dequeueReusableCell(withIdentifier: "PartyCell", for: indexPath) as? PartyCell {
            let partyRock = partyRocks[indexPath.row]
            return cell
        } else {
            return UITableViewCell()
        }
    }
}

还要注意,在viewcontroller中复制numberofrowsinsection方法两次时会遇到问题,所以我删除了一个......