Swift 报错 Type 'xx' does not conform to protocol 'UITableViewDataSource'

Swift中当你设置完UITableView的代理和数据源

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这个协议, 但仍然报下面的错误.
Type 'ViewController' does not conform to protocol 'UITableViewDataSource'

Why ?

因为你没有实现协议里面的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
    }


}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 因为你没有实现协议里面的Requred方法, 所以提示抱错。。。 两个方法:cellforrow和numberof...
    Johnny_Chang阅读 1,183评论 0 1
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,198评论 4 61
  • 岌岌而立之年,耳闻了世事苍茫,目见了形势迷离,至此,也多少参半了处世的酸甜。 人丑多读书,处世多读书,这是自然。读...
    高明楼阅读 332评论 0 1
  • 作者/春晓 1 周三原本是一名体育老师,就职在县城一家中学,他长得人高马大,虎背熊腰,颇像日本的相扑选手,但不知道...
    清沫阅读 718评论 0 2