簡體   English   中英

類型的viewcontroller不符合協議uitableviewdatasource

[英]type viewcontroller does not conform to protocol uitableviewdatasource

我已經在另一視圖中完成了相同的tableview,但是在此視圖中無法正常工作。 我也在尋找解決方案,但我不知道什么是行不通的

class HistoryViewController:UIViewController,UITableViewDelegate,UITableViewDataSource {

@IBOutlet weak var tableview: UITableView!

var date = ["ciao", "muori"]
var place = ["aaaa"]



override func viewDidLoad() {
    super.viewDidLoad()

    tableview.dataSource = self
    tableview.delegate = self

    // Do any additional setup after loading the view.
}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

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

func tableview(tableView: UITableView, cellForRowIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = self.tableview.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! HistoryCell
    cell.placeLabel.text = place[indexPath.row]
    cell.dateLabel.text = date[indexPath.row]
    return cell

}


func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{

}

有人可以幫助我嗎?

  • 轉到問題導航器( ⌘4 )。
  • 打開錯誤消息旁邊的顯示三角形。
  • 實現錯誤消息下方顯示的缺少的必需方法。

並使用代碼補全來避免這些拼寫錯誤。

您在方法名稱中犯了一個錯誤:您必須實現tableView:cellForRowAtIndexPath:而不是tableview:cellForRowIndexPath: 這是一個正確的實現:

class HistoryViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var tableview: UITableView!

    var date = ["ciao", "muori"]
    var place = ["aaaa"]

    override func viewDidLoad() {
        super.viewDidLoad()

        tableview.dataSource = self
        tableview.delegate = self

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

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

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = self.tableview.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! HistoryCell
        cell.placeLabel.text = place[indexPath.row]
        cell.dateLabel.text = date[indexPath.row]
        return cell
    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
    {
    }
}

請檢查是否

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

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

插入在{}類括號內。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM