簡體   English   中英

類型“ ViewController”不符合協議“ UITableViewDataSource”

[英]Type “ViewController” does not conform to protocol 'UITableViewDataSource"

我在編碼時遇到錯誤,說

類型“ ViewController”不符合協議“ UITableViewDataSource”

誰能說出這是怎么回事?

import UIKit

class ViewController: UIViewController, UITableViewDataSource{

    let devCourses = [
        ("Math"),
        ("Science"),
        ("English"),
        ("Computer Programming"),
        ("Physics")]
    func numberOfSectionsInTableview(tableview: UITableView)-> Int {
        return 1
    }
    func tableview(tableView: UITableView, numberOfRowsInSection section: Int) ->Int{
         return devCourses.count
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = UITableViewCell()

        let (courseTitle) = devCourses[indexPath.row]

        cell.textLabel?.text = courseTitle
        return cell
    }

    override func viewDidLoad() {
        super.viewDidLoad()

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

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

我認為您缺少代表所需的一些功能:

聲明應更改為包括:

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

然后確保表的所有必需方法都在其中:

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

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

並在ViewDidLoad上調用委托

override func viewDidLoad() {
    super.viewDidLoad()

      yourtableview.dataSource = self

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

希望這可以幫助

這是一個常見錯誤,請確保使用正確的單詞。 在您的情況下,應為:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
     return devCourses.count
}

func tableview(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return devCourses.count
}

您必須使用V iew而不是v iew 區分大小寫

暫無
暫無

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

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