最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]Swift中虽已实现UITableViewDataSource的必须的函数但仍报错:Type does not conform to protocol UITableViewDataSource

Swift crifan 3657浏览 0评论
很是奇怪:
此处已经实现了对应的UITableViewDataSource中的函数:
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        //print("numberOfRowsInSection indexPath = \(indexPath)")
        return switchTeamListTitleArr.count
    }

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        //print("heightForRowAtIndexPath indexPath = \(indexPath)")
        return HeightSwitchTeamCell
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

但是却还是报错:

Type ‘TeamTableViewData’ does not conform to protocol ‘UITableViewDataSource’

Type TeamTableViewData does not conform to protocol UITableViewDataSource
 Protocol requires function ‘tableView(_:numberOfRowsInSection:)’ with type ‘(UITableView, numberOfRowsInSection: Int) -> Int’ (UIKit.UITableViewDataSource)
Protocol requires function tableView numberOfRowsInSection with type UITableView numberOfRowsInSection Int Int UIKit UITableViewDataSource
搜:
swift does not conform to protocol UITableViewDataSource
好像是:
定义的时候,后面只有protocol 所以出错?
参考:
提到了“But realize that the super class must be the first item in the comma separated list.”
再去找官网的Protocol的解释
果然是:
如果类有父类,则集成的父类要放在右边第一个,后面再跟着protocol:
class SomeClass: SomeSuperclass, FirstProtocol, AnotherProtocol {
    // class definition goes here
}

而我此处,好像只需要实现对应的类即可,所以,去试着改为struct的形式:

struct SomeStructure: FirstProtocol, AnotherProtocol {
    // structure definition goes here
}
看看效果:
struct TeamTableViewData: UITableViewDataSource, UITableViewDelegate{

直接出错:

JianDao/TeamTableViewData.swift:11:8: Non-class type ‘TeamTableViewData’ cannot conform to class protocol ‘UITableViewDataSource’
所以还是用class吧。
用:
class TeamTableViewData: UIViewController, UITableViewDataSource, UITableViewDelegate{

即可解决问题,通过编译。

[总结]
此处,想要实现UITableViewDataSource, UITableViewDelegate,写了个类:
class TeamTableViewData: UITableViewDataSource, UITableViewDelegate{
虽然内部已经实现了UITableViewDataSource的必须的函数:
tableView(_:cellForRowAtIndexPath:) Required
numberOfSectionsInTableView(_:)
tableView(_:numberOfRowsInSection:) Required
sectionIndexTitlesForTableView(_:)
tableView(_:sectionForSectionIndexTitle:atIndex:)
tableView(_:titleForHeaderInSection:)
tableView(_:titleForFooterInSection:)
的:
tableView(_:cellForRowAtIndexPath:) Required
tableView(_:numberOfRowsInSection:) Required
但是还是报错:
does not conform to protocol ‘UITableViewDataSource’
的原因是:
如果是一个class,想要实现UITableViewDataSource的话,则需要加水对应的父类:
class TeamTableViewData: UIViewController, UITableViewDataSource, UITableViewDelegate{
才可以
-》这样才符合实现protocol所要求的语法

转载请注明:在路上 » [已解决]Swift中虽已实现UITableViewDataSource的必须的函数但仍报错:Type does not conform to protocol UITableViewDataSource

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
86 queries in 0.174 seconds, using 22.10MB memory