![](/img/trans.png)
[英]Type “ViewController” does not conform to protocol 'UITableViewDataSource"
[英]type TypeViewOne does not conform to protocol UITableViewDataSource
这是我的代码
class TableViewOne: UITableViewDataSource, UITableViewDelegate {
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("firstCell") as! firstCell
let index = indexPath.row
cell.labelMy.text = ViewController.firstData![index]
return cell
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let s = ViewController.firstData {
return s.count
}
return 0
}
}
我不知道为什么这次出现这个错误,尽管当我使用UITableViewController
的子类时,这些就足够了,听起来像UITableViewControlelr
已经为其他必需的函数实现了,但是我不知道它们是我自己。
UITableViewDataSource
要求您遵循NSObject
协议,而TableViewOne
(纯swift类)则不遵循。 更改声明以从NSObject
继承,如下所示:
class TableViewOne: NSObject, UITableViewDataSource, UITableViewDelegate
其次,您不需要override
关键字,因为TableViewOne
不会将另一个对象作为子类。 UITableViewController
的默认实现为返回1,但由于您没有将UITableViewController
为子类,因此无法重写任何方法。
override func numberOfSectionsInTableView(tableView: UITableView) -> Int
与
func numberOfSectionsInTableView(tableView: UITableView) -> Int
看来您已经在情节提要中使用了UIViewController并向其中添加了UITableView,如果是这样,则子类化UIViewController并像这样更改您的类
class TableViewOne: UIViewController,UITableViewDataSource, UITableViewDelegate {
如果您在情节提要中使用了TableViewController,则仅需要子类化UITableViewController
class TableViewOne: UITableViewController{
&您只需要添加以下功能即可确认tableview数据源
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 0
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 0
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.