Swift 報錯 Type 'xx' does not conform to protocol 'UITableViewDataSource'

Swift中當你設(shè)置完UITableView的代理和數(shù)據(jù)源

class ViewController: UIViewController,MAMapViewDelegate,AMapSearchDelegate,
UITableViewDelegate,UITableViewDataSource {

 lazy var tableView: UITableView! = {
        var tableView = UITableView(frame: CGRectZero, style: UITableViewStyle.Grouped)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.registerClass(UITableViewCell.classForCoder(), forCellReuseIdentifier: "Cell")
        return tableView
    }()

}

并且你也添加了UITableViewSource這個協(xié)議, 但仍然報下面的錯誤.
Type 'ViewController' does not conform to protocol 'UITableViewDataSource'

Why ?

因為你沒有實現(xiàn)協(xié)議里面的Requred 方法, 所以提示抱錯, 不過個人覺得這樣的報錯提示很不友好...

DAD132FC-38A9-491E-8C97-5EB40267DF59.png

最后你的代碼改成如下, 報錯就會消失.

class ViewController: UIViewController,MAMapViewDelegate,AMapSearchDelegate,
UITableViewDelegate,UITableViewDataSource {

   lazy var tableView: UITableView! = {
        var tableView = UITableView(frame: CGRectZero, style: UITableViewStyle.Grouped)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.registerClass(UITableViewCell.classForCoder(), forCellReuseIdentifier: "Cell")
        return tableView
    }()
   //MARK: UITableViewDelegate
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
        return cell
    }


}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 因為你沒有實現(xiàn)協(xié)議里面的Requred方法, 所以提示抱錯。。。 兩個方法:cellforrow和numberof...
    Johnny_Chang閱讀 1,226評論 0 1
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,037評論 4 61
  • 岌岌而立之年,耳聞了世事蒼茫,目見了形勢迷離,至此,也多少參半了處世的酸甜。 人丑多讀書,處世多讀書,這是自然。讀...
    高明樓閱讀 412評論 0 1
  • 作者/春曉 1 周三原本是一名體育老師,就職在縣城一家中學,他長得人高馬大,虎背熊腰,頗像日本的相撲選手,但不知道...
    清沫閱讀 783評論 0 2

友情鏈接更多精彩內(nèi)容