简体   繁体   English

FirstViewController不符合协议

[英]FirstViewController does not conform to Protocol

I wrote this in Xcode 6 (Swift) but it says "Type 'FirstViewController' does not conform to protocol 'UITableViewDataSource'" and won't let me build the program. 我在Xcode 6(Swift)中编写了此代码,但是它说“类型'FirstViewController'不符合协议'UITableViewDataSource'”,并且不会让我构建程序。 Please help? 请帮忙?

import UIKit

class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

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

//UIViewTableDataSource
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int{
   return taskMGR.tasks.count
}


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



        let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier:
            "test")

        cell.textLabel?.text = taskMGR.tasks[indexPath.row].name
        cell.detailTextLabel?.text = taskMGR.tasks[indexPath.row].desc

        return cell
}

}

I rewrote your class to work. 我改写了你的班上课。 I deleted a couple of variables I did not need, but you can add them back. 我删除了一些我不需要的变量 ,但是您可以将它们重新添加。 Key was to delete ' UITableViewDataSource ' (you do not conform to this) and to unwrap the optional cell the way you wrote it. 关键是删除“ UITableViewDataSource ”(您不符合此要求)并按照编写它的方式打开可选单元格。 I prefer not to construct the cell that way, but that is another discussion. 我不想以这种方式构造该单元,但这是另一种讨论。 If you still have issues let me know. 如果您仍有问题,请告诉我。

import UIKit

class FirstViewController: UIViewController, UITableViewDelegate {

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

//UIViewTableDataSource
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int{
    return 1
}


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

        let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier:
            "test")!

        return cell
       }

}

As I wrote in the comments you might as well change the class to a UITableViewController subclass as it is basically the same as UIViewController + UITableViewDelegate + UITableViewDataSource (with a little bit of extra functionality included if you want it). 正如我在评论中所写,您最好将类更改为UITableViewController子类,因为它基本上与UIViewController + UITableViewDelegate + UITableViewDataSource (如果需要,还包括一些额外的功能)。 It also has a UITableView property included "out of the box". 它还具有“开箱即用”的UITableView属性。

You will then end up with the following class: 然后,您将获得以下课程:

class FirstViewController: UITableViewController {

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

    //UIViewTableDataSource
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        return taskMGR.tasks.count
    }


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
            let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier:"test")
            cell.textLabel?.text = taskMGR.tasks[indexPath.row].name // You can remove ? when updating to XCode 6.1 / Swift 1.1
            cell.detailTextLabel?.text = taskMGR.tasks[indexPath.row].desc
            return cell
    }
}

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

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