ios 类型“ViewController”不符合协议“UITableViewDataSource”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25581780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Type 'ViewController' does not conform to protocol 'UITableViewDataSource'
提问by Code cracker
Started practice swift. In singleViewController I am trying to make a UITableView
. In storyboard I set the datasource and delegate. Here I am getting the error * 'ViewController' does not conform to protocol 'UITableViewDataSource' *
开始快速练习。在 singleViewController 我试图制作一个UITableView
. 在故事板中,我设置了数据源和委托。在这里我收到错误*'ViewController' 不符合协议 'UITableViewDataSource' *
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var table: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
func numberOfSectionsInTableView(tableView: UITableView!) -> Int
{
return 20
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!
{
let cell:UITableViewCell=UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "mycell")
cell.textLabel.text="row#\(indexPath.row)"
cell.detailTextLabel.text="subtitle#\(indexPath.row)"
return cell
}
回答by
You should implement all the required methods before the last }
, but you have written them outside of the UIViewController. Also, you need to change the func for number of lines.
您应该在 last 之前实现所有必需的方法}
,但是您已经在 UIViewController 之外编写了它们。此外,您需要更改行数的功能。
the suggested edit
建议的编辑
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var table: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView:UITableView!, numberOfRowsInSection section:Int) -> Int
{
return 20
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!
{
let cell:UITableViewCell=UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "mycell")
cell.textLabel.text="row#\(indexPath.row)"
cell.detailTextLabel.text="subtitle#\(indexPath.row)"
return cell
}
}
回答by aalesano
Try removing the ! on your func. That did the job for me
尝试删除!在你的功能上。那对我有用
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell:UITableViewCell=UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "mycell")
cell.textLabel.text="row#\(indexPath.row)"
cell.detailTextLabel.text="subtitle#\(indexPath.row)"
return cell
}
回答by staticVoidMan
You need to implement all the required methods of UITableViewDataSource
in order to get rid of that error.
您需要实现所有必需的方法UITableViewDataSource
才能消除该错误。
Basically... you're missing:
基本上......你错过了:
func tableView(tableView:UITableView!, numberOfRowsInSection section:Int) -> Int {
//return XX
}
回答by Muhammad Umar
I had the same problem, things would workout rather easily in Objective-C, probably because we are more familiar with it at the moment, but in this case, swift is very new, therefore, the its error notifications are rather vague.
我遇到了同样的问题,在 Objective-C 中事情会很容易解决,可能是因为我们现在更熟悉它,但在这种情况下,swift 是非常新的,因此,它的错误通知相当模糊。
While I was implementing the UITableView based application, and I ran into this problem. I opened up the implementation file for UITableView by pressing command and clicking on UITableView. In implementation file, we can clearly see that two functions are mandatory to implement,
在我实现基于 UITableView 的应用程序时,我遇到了这个问题。我通过按下命令并单击 UITableView 打开了 UITableView 的实现文件。在实现文件中,我们可以清楚地看到两个函数是强制实现的,
- func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
- func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
- func tableView(tableView: UITableView, numberOfRowsInSection 部分: Int) -> Int
- func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
I arrived at this post and started to put things together while keeping my meager knowledge of objective-C programming in mind. Reason for the error being that a table view is defined by two elements, first the section and rows in a section, and second the tableview cells. By default there is at least one section in the table view, but we need conformance about number of rows in a section. Secondly, we need to know which cell are we going to present in a particular row in a section. Regardless,even if we are using default UITableViewCell, we still need an identifier to access it in order to set its subviews or properties. I hope it was helpful, A bit soft criticism will be appreciated since I am myself very new to Swift :)
我来到这篇文章并开始把事情放在一起,同时牢记我对 Objective-C 编程的微薄知识。错误的原因是表格视图由两个元素定义,首先是部分和部分中的行,其次是表格视图单元格。默认情况下,表视图中至少有一个节,但我们需要关于节中行数的一致性。其次,我们需要知道我们将在一个部分的特定行中呈现哪个单元格。无论如何,即使我们使用默认的 UITableViewCell,我们仍然需要一个标识符来访问它以设置它的子视图或属性。我希望它有帮助,因为我自己对 Swift 很陌生,所以我会感谢一些软批评:)
回答by MB_iOSDeveloper
The following code didn't work for me on iOS 8.1. in XCode 6.1.1. This code works:
以下代码在 iOS 8.1 上对我不起作用。在 XCode 6.1.1 中。此代码有效:
import UIKit
class ViewController : UIViewController,UITableViewDelegate,UITableViewDataSource {
override func viewDidLoad() {
super.viewDidLoad()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
//currently only a testing number
return 25
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
var cell:UITableViewCell=UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "mycell")
cell.textLabel?.text = "row#\(indexPath.row)"
cell.detailTextLabel?.text = "subtitle#\(indexPath.row)"
return cell
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
回答by Sanju
Just Add these two methods then that error will be resolved[XCode8 Swift3]
只需添加这两个方法即可解决该错误[XCode8 Swift3]
first method :
第一种方法:
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
}
Second Method :
第二种方法:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
}
回答by Yuting
I had just faced the same problem in Swift.
我刚刚在 Swift 中遇到了同样的问题。
You should realize all the functions for the UITableViewDataSource in the class, which means the following functions should be realized:
你应该实现类中 UITableViewDataSource 的所有功能,也就是说应该实现以下功能:
func numberOfSectionsInTableView(tableView: UITableView) -> Int{}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {}
I'm not sure whether missing the function for numberOfSectionsInTableView works for you or not. For me, I have to realize it in my class.
我不确定缺少 numberOfSectionsInTableView 的功能是否适合您。对我来说,我必须在我的课堂上意识到这一点。
回答by Yunus Nedim Mehel
This is probably caused by a typo or wrong styling on the method name. I used to cmd + left click on UITableView
and copy & paste method names on my UIViewController
; which won't work on Swift
.
这可能是由方法名称上的拼写错误或样式错误引起的。我曾经使用 cmd + 左键单击UITableView
并在我的UIViewController
;上复制和粘贴方法名称;这不适用于Swift
.
Instead, type func tableView
, look for the desired method on the list and let auto complete do its job.
相反,键入func tableView
,在列表中查找所需的方法并让自动完成完成它的工作。
回答by KawaiKx
This is a common warning which means that " you haven't implemented the required methods of the protocol yet"
这是一个常见的警告,表示“您尚未实现协议所需的方法”
A view object on the storyboard may need a datasource. For example, TableView needs a datasource and usually, View Controller acts as one.
故事板上的视图对象可能需要数据源。例如,TableView 需要一个数据源,通常,视图控制器充当一个。
So Table View expects the ViewController to contain the methods which return the 'must have' information for the table view.
所以表视图期望 ViewController 包含返回表视图的“必备”信息的方法。
Table view needs to know the number of sections, number of rows in each section etc..
表视图需要知道节的数量,每个节的行数等。
Unless all the required information is returned by the datasource object, the warning will persist.
除非数据源对象返回所有必需的信息,否则警告将持续存在。
回答by Ankit
Change the syntax of "UITableViewDataSource" protocol required methods as per new swift 3 documentation:
根据新的 swift 3 文档更改“UITableViewDataSource”协议所需方法的语法:
internal func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
内部 func tableView(_ tableView: UITableView, numberOfRowsInSection 部分: Int) -> Int
internal func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
内部 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
This has worked for me while compiling with swift 3 compiler
这在使用 swift 3 编译器编译时对我有用