类型“ViewController”不符合协议“UITableViewDataSource”

时间:2015-01-30 17:22:31

标签: swift

我在尝试快速学习的前几页中已经分解了。得到这个错误。

这是代码。我做错了什么基本的事情?

class ViewController : UIViewController,UITableViewDataSource,UITableViewDelegate {

    var restaurantNames = ["Cafe Deadend", "Homei", "Teakha", "Cafe Loisl", "Petite Oyster", "For Kee Restaurant", "Po's Atelier", "Bourke Street Bakery", "Haigh’s Chocolate", "Palomino Espresso",
        "Upstate", "Traif", "Graham Avenue Meats And Deli", "Waffle &  Wolf", "Five Leaves", "Cafe Lore", "Confessional", "Barrafina", "Donostia", "Royal Oak", "CASK Pub and Kitchen"]

    func tableView(tableView: UITableView, NumberOfRowsInSection section: Int) -> Int {
        //Return the number of rows in the section
        return restaurantNames.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)  -> UITableViewCell {

        let cellIdentifier = "Cell"
        let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as UITableViewCell

        // Configure the cell...

        cell.textLabel?.text = restaurantNames[indexPath.row]
        return cell
    }

    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.
    }

}

2 个答案:

答案 0 :(得分:4)

numberOfRowsInSection中有一个大写的N.应该是:

   func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        //Return the number of rows in the section
        return restaurantNames.count
    }

答案 1 :(得分:0)

每当您收到此错误时,表示您所制作的课程不符合您所说的继承的协议。

这是什么意思?这意味着您继承了UITableViewDataSource并且具有相关联的协议。它希望您创建的方法符合其协议(要求)。

在这里查看此课程的要求: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDataSource_Protocol/index.html

通过2种方法查看所需的位置:

tableView(_:cellForRowAtIndexPath:)
 Required


and


tableView(_:numberOfRowsInSection:)
 Required

实施这些方法,你会好起来的!