[英]Type “ViewController” does not conform to protocol 'UITableViewDataSource"
[英]type viewcontroller does not conform to protocol uitableviewdatasource
我已经在另一视图中完成了相同的tableview,但是在此视图中无法正常工作。 我也在寻找解决方案,但我不知道什么是行不通的
class HistoryViewController:UIViewController,UITableViewDelegate,UITableViewDataSource {
@IBOutlet weak var tableview: UITableView!
var date = ["ciao", "muori"]
var place = ["aaaa"]
override func viewDidLoad() {
super.viewDidLoad()
tableview.dataSource = self
tableview.delegate = self
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView:UITableView, numberOfRowsInSection section:Int) -> Int
{
return 1
}
func tableview(tableView: UITableView, cellForRowIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableview.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! HistoryCell
cell.placeLabel.text = place[indexPath.row]
cell.dateLabel.text = date[indexPath.row]
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
}
有人可以帮助我吗?
并使用代码补全来避免这些拼写错误。
您在方法名称中犯了一个错误:您必须实现tableView:cellForRowAtIndexPath:
而不是tableview:cellForRowIndexPath:
这是一个正确的实现:
class HistoryViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableview: UITableView!
var date = ["ciao", "muori"]
var place = ["aaaa"]
override func viewDidLoad() {
super.viewDidLoad()
tableview.dataSource = self
tableview.delegate = self
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableview.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! HistoryCell
cell.placeLabel.text = place[indexPath.row]
cell.dateLabel.text = date[indexPath.row]
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
}
}
请检查是否
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
和
func tableView(tableView:UITableView, numberOfRowsInSection section:Int)
插入在{}类括号内。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.