不符合协议UITableViewDataSource

时间:2014-11-25 20:23:10

标签: uitableview swift

我想在swift中执行表视图,并且我收到以下错误

  

"不符合协议UITableViewDataSource"

任何人都可以提出我出错的地方吗?

import UIKit

class WalkthroughScreen2ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{

   let cityArray = ["Portland","San Francisco","Cupertino"]
    override func viewDidLoad() {
        super.viewDidLoad()
        self.createReminderLabel()



        // Create our UITableView with our view's frame
        var tableView: UITableView = UITableView(frame: self.view.frame)


        // Register our cell's class for cell reuse
        tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")

        // Set our source and add the tableview to the view
        tableView.dataSource = self
        self.view.addSubview(tableView)
   }



    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
        return cityArray.count
    }


    func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
        // dequeue a cell for the given indexPath
        var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell

        // set the cell's text with the new string formatting
        cell.textLabel.text = "\(cityArray[indexPath.row])"

        return cell
    }
}

1 个答案:

答案 0 :(得分:0)

cellForRowAtIndexPath的签名在您的代码中不正确,应该是:

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

区别在于tableView不是可选的。您还必须实现此方法:

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

同样,numberOfRowsInSection签名是:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
相关问题