繁体   English   中英

Xcode类型“ ViewController”中出现错误不符合协议“ UITableViewDataSource”

[英]Getting error in Xcode type “ViewController” does not conform to protocol“UITableViewDataSource”

我正在尝试遵循表视图教程,但是在类声明的行上出现错误:“类型“ ViewController”不符合协议“ UITableViewDataSource””,这是我的代码:

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    private let dwarves = ["Sleepy", "Sneezy", "Bashful", "Happy", "Doc", "Grumpy", "Dopey", "Thorin", "Dorin", "Nori", "Ori", "Balin", "Dwalin", "Fili", "Kili", "Oin", "Gloin", "Bifur", "Bofur", "Bombur"]

    let simpleTableIdentifier = "SimpleTableIdentifier"

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCellWithIdentifier(simpleTableIdentifier) as? UITableViewCell
        if cell == nil {
            cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: simpleTableIdentifier)
        }

        cell!.textLabel!.text = dwarves[indexPath.row]
        return cell!
    }


}

您错过了func tableView(tableView: UITableView, numberOfRowsInSection secion: Int) -> Int {}

这对于表视图是必需的。

因此,只需将其添加到您的代码中:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return count(dwarves)
}

您缺少数据源协议的必需方法: tableView:numberOfRowsInSection:

请参阅: https//developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDataSource_Protocol/

UITableViewDataSource具有两个必需的方法:

tableView:cellForRowAtIndexPath:

tableView:numberOfRowsInSection:

两者都需要实现。

您缺少第二个。

你可以在这里找到UITableViewDataSource更多信息: 这里

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM