does not confirm to protocol UITableViewDataSource

2020-05-08 08:54发布

问题:

Here is my Code , Compiler is still showing error although I wrote implementation of both methods

Please tell why

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

        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
        }

    }

Here is screenshot of Xcode

回答1:

You have probably written this code in a previous version of Xcode. The correct signature for the 2 methods has changed, and it's now:

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

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

The tableView parameter is no longer an optional.