Type 'ViewController' does not conform to protocol 'UITableViewDataSource'

后端 未结 17 2610
旧巷少年郎
旧巷少年郎 2020-11-29 03:02

Started practice swift. In singleViewController I am trying to make a UITableView. In storyboard I set the datasource and delegate. Here I am getting the error

17条回答
  •  时光说笑
    2020-11-29 03:24

    with some edits suggested by auto complete, from the above answer by @MB_iOSDeveloper on swift 3, Xcode 8.3.2, this code works for me:

    class MenuViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        //currently only a testing number
        return 25
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
    
        var cell:UITableViewCell=UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: "mycell")
        cell.textLabel?.text = "row#\(indexPath.row)"
        cell.detailTextLabel?.text = "subtitle#\(indexPath.row)"
        return cell
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    }

提交回复
热议问题