繁体   English   中英

与“类型'ViewController'不符合协议'UITableViewDataSource'”相关的编译和构建错误

[英]Compile & Build error related to “Type 'ViewController' does not conform to protocol 'UITableViewDataSource'”

我不明白为什么我的应用程序无法编译。 这是当前的输出:

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var IndexArray = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
    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.
    }

    func numberOfSectionsinTableView(tableView: UITableView) -> Int {
        return IndexArray.count
    }

    func tableView(tableView: UITableView, tiltleForHeaderInSection section: Int) -> String? {
        return IndexArray[section]
    }

    func sectionIndexTitlesfortableView (tableView: UITableView) -> [String]? {
        return IndexArray
    }

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

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TableCell", for: indexPath as IndexPath) as! TableCell

        cell.imgPhoto.image = UIImage(named: "charity")
        cell.lblUserName.text! = "User Name"

        return cell
    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    }
}

您缺少指定在类派生协议中声明的一些方法的信息。

func tableView(UITableView,cellForRowAt:IndexPath)必需。 要求数据源在单元格中插入表格视图的特定位置。

func tableView(UITableView,numberOfRowsInSection:Int)必需。 告诉数据源返回表视图给定部分中的行数。

至少必须在您的类中声明上述两个方法,否则会出现错误。

这些只是必需的方法,但是为了以正确的方式运行,您需要定义其他方法。 请参阅Apple文档中的UITableViewDataSource协议

在Swift 3中,所有方法签名已更改为:

func numberOfSections(in tableView: UITableView) -> Int { }

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { }

func sectionIndexTitles(for tableView: UITableView) -> [String]? { }

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { }

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { }

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {}

暂无
暂无

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

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