Ios “类型”;“视图控制器”;不符合协议';UITableViewDataSource“;
我在编码时出错,说 类型“ViewController”不符合协议“UITableViewDataSource” 谁能说出这出了什么问题Ios “类型”;“视图控制器”;不符合协议';UITableViewDataSource“;,ios,swift,Ios,Swift,我在编码时出错,说 类型“ViewController”不符合协议“UITableViewDataSource” 谁能说出这出了什么问题 import UIKit class ViewController: UIViewController, UITableViewDataSource{ let devCourses = [ ("Math"), ("Science"), ("English"), ("Computer P
import UIKit
class ViewController: UIViewController, UITableViewDataSource{
let devCourses = [
("Math"),
("Science"),
("English"),
("Computer Programming"),
("Physics")]
func numberOfSectionsInTableview(tableview: UITableView)-> Int {
return 1
}
func tableview(tableView: UITableView, numberOfRowsInSection section: Int) ->Int{
return devCourses.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell()
let (courseTitle) = devCourses[indexPath.row]
cell.textLabel?.text = courseTitle
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
我认为您缺少代表要求的一些功能: 声明应更改为包括:
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
然后确保表的所有必需方法都存在:
func tableView(tableView:UITableView!, numberOfRowsInSection section:Int) -> Int
及
并在ViewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
yourtableview.dataSource = self
// Do any additional setup after loading the view.
}
希望这有帮助这是一个常见的错误,请确保使用正确的单词。在您的情况下,它应该是:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return devCourses.count
}
不是
您必须使用tableView而不是tableView。它区分大小写您是否尝试将父级更改为UITableViewController?我将“tableview”改为“tableview”,这就是我出错的原因。谢谢大家的帮助!
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return devCourses.count
}
func tableview(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return devCourses.count
}