Swift2中的UITableViewDataSource错误

时间:2015-09-17 17:39:43

标签: uitableview swift2 xcode7

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

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

1 个答案:

答案 0 :(得分:2)

如果某个类不符合协议,则意味着您需要实现一些协议方法。如果是 UITableViewDataSource ,则需要实现以下方法:

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

<强>的tableView(_:numberOfRowsInSection:)

它定义了每个tableViewCell的设置

<强>的tableView(_:的cellForRowAtIndexPath:)

也许你忘了实现一个或两个dataSource方法。

E.g:

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/

相关问题