【问题标题】:FirstViewController does not conform to ProtocolFirstViewController 不符合协议
【发布时间】:2014-10-20 19:04:47
【问题描述】:

我在 Xcode 6 (Swift) 中编写了这个,但它说“类型 '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
}

}

【问题讨论】:

  • 你在这两个dataSource方法前面缺少override...
  • 另外:与其遵循UIViewControllerUITableViewDelegateUITableViewDataSource,不如让该类成为UITableViewController 的子类...
  • 在 viewDidLoad 中以编程方式设置数据源和委托

标签: ios


【解决方案1】:

正如我在 cmets 中所写,您不妨将类更改为 UITableViewController 子类,因为它与 UIViewController + UITableViewDelegate + UITableViewDataSource 基本相同(带有 little 如果需要,还包括一些额外的功能)。它还有一个“开箱即用”的 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
    }
}

【讨论】:

    【解决方案2】:

    我重写了你的类来工作。我删除了几个我不需要的变量,但您可以将它们添加回来。关键是删除'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
           }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多