Ios FirstViewController不符合协议
我是在Xcode 6(Swift)中写的,但是它说“类型‘FirstViewController’不符合协议‘UITableViewDataSource’”,并且不允许我构建程序。请帮忙Ios FirstViewController不符合协议,ios,Ios,我是在Xcode 6(Swift)中写的,但是它说“类型‘FirstViewController’不符合协议‘UITableViewDataSource’”,并且不允许我构建程序。请帮忙 import UIKit class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { override func viewDidLoad() { super.viewDidLoad
import UIKit
class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
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.
}
//UIViewTableDataSource
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int{
return taskMGR.tasks.count
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) ->
UITableViewCell!{
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier:
"test")
cell.textLabel?.text = taskMGR.tasks[indexPath.row].name
cell.detailTextLabel?.text = taskMGR.tasks[indexPath.row].desc
return cell
}
}
我重写了你的课程,让你继续学习。我删除了几个我不需要的变量,但您可以将它们添加回去。关键是删除“
UITableViewDataSource
”(您不符合此要求),并以编写可选单元格的方式将其展开。我不希望这样构造单元格,但这是另一种讨论。如果你还有问题,请告诉我
import UIKit
class FirstViewController: UIViewController, UITableViewDelegate {
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.
}
//UIViewTableDataSource
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int{
return 1
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) ->
UITableViewCell!{
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier:
"test")!
return cell
}
}
正如我在评论中所写的,您最好将类更改为
UITableViewController
子类,因为它基本上与UIViewController
+UITableViewDelegate
+UITableViewDataSource
(如果需要,还包括一些额外的功能)。它还包含“开箱即用”的UITableView属性
然后,您将得到以下类:
class FirstViewController: UITableViewController {
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.
}
//UIViewTableDataSource
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return taskMGR.tasks.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier:"test")
cell.textLabel?.text = taskMGR.tasks[indexPath.row].name // You can remove ? when updating to XCode 6.1 / Swift 1.1
cell.detailTextLabel?.text = taskMGR.tasks[indexPath.row].desc
return cell
}
}
在这两种数据源方法之前,您缺少
覆盖
。。另外:UITableViewController
,UITableViewDelegate
,UITableViewDataSource
您还可以将类设为UITableViewController
的子类……在viewdiload中以编程方式设置数据源和委托