FirstViewController不符合Protocol

时间:2014-10-20 19:04:48

标签: ios

我在Xcode 6(Swift)中写过这个,但它说“Type'FirstViewController'不符合协议'UITableViewDataSource'”,并且不会让我构建程序。请帮帮忙?

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
}

}

2 个答案:

答案 0 :(得分:0)

我重写了你的课程。我删除了一些我不需要的变量,但您可以将它们添加回去。关键是删除' UITableViewDataSource' (你不符合这一点)并以你编写它的方式展开可选单元格。我不想以这种方式构建单元格,但这是另一种讨论。如果您仍有问题,请告诉我。

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
       }

}

答案 1 :(得分:0)

正如我在评论中写的那样,您可以将班级更改为UITableViewController子类,因为它与UIViewController + UITableViewDelegate + UITableViewDataSource基本相同(与如果你需要,可以包含位的额外功能。它还有一个UITableView属性,包括“开箱即用”。

然后您将结束以下课程:

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