Swift2 中的 UITableViewDataSource 错误

UITableViewDataSource error in Swift2

我正在学习 Swift 并且在编写一些代码时,屏幕上出现错误显示:键入 JVViewController (class name) 不符合协议 'UITableViewDataSource'。如果我在其他应用程序中做的完全相同并且我从未遇到过此类问题,我不知道为什么会出现在我的文件中。

请让我知道如何解决这种情况。 谢谢!

如果class不符合某个协议,说明你需要实现一些协议方法。在 UITableViewDataSource 的情况下,需要实现以下方法:

它定义了将在 tableView 上显示的 tableViewCell 的数量

tableView(_:numberOfRowsInSection:)

它定义了每个tableViewCell的设置

tableView(_:cellForRowAtIndexPath:)

也许您忘记实现一个或这两个数据源方法。

例如:

class JVViewController : UIViewController, UITableViewDelegate, UITableViewDataSource {

    ...

    var collectionItems = ["One", "Two", "Three"]    

    ...

    //Quantity of rows
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.collectionItems.count;
    }

    //Row settings
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell

        cell.textLabel?.text = self.collectionItems[indexPath.row]

        return cell
    }

    ...

}

您可以在 Apple 文档中找到更多信息:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDataSource_Protocol/