2017-06-16 66 views
-3

我正在學習本教程,當我使用其中提到的代碼時,我得到了下面的圖像-1中顯示的錯誤,我不明白爲什麼我儘管得到這些錯誤我使用相同的代碼如何解決「使用未聲明的類型」

我GOOGLE瞭如何解決這個錯誤,有的帖子建議導入UKIT類,而我做了,但問題仍然存在

請讓我知道如何解決它

image-1

enter image description here

+0

'UITableViewDataSource'不是'UITableViewSource',這就是Swift,而不是Objective-C – Larme

+0

你需要編寫UITableViewDelegate和UITableViewDataSource。當你寫了UITableViewSource。這是不正確的。 – Surjeet

回答

0

使用UITableViewDataSource代替UITableViewSource

並添加代表和數據源

//MARK: - Table Delegates 

    func numberOfSections(in tableView: UITableView) -> Int { 
     return 1; 
    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return 10; 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 


     let cellId: NSString = "cellSearch" 
     let cell: customCell = tableView.dequeueReusableCell(withIdentifier: cellId as String)! as! customCell 

     return cell 

    } 

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 
     return 0 

    } 

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 



    } 
+0

好的..我按你的建議..現在我得到這個錯誤:類型'ViewController'不符合協議'UITableViewDataSou rce' – user2121

+0

添加所有必需的tableview代表 – Bali

+0

更新了答案 – Bali

0

你有拼寫錯誤UITableViewSource它實際上UITableViewDataSource,

添加數據源的方法,

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = demoTableVIew.dequeueReusableCell(withIdentifier: "cell", for: indexPath) 
     cell.textLabel?.text = "CELL NUMBER : \(indexPath.row)" 

     return cell 
    } 
+0

行..我按照你的建議..現在我得到這個錯誤:類型'ViewController'不符合協議'UITableViewDataSource' – user2121

+0

func tableView(_ tableView:UITableView,numberOfRowsInSection部分:Int) - > Int { return計數 } FUNC的tableView(_的tableView:UITableView的,cellForRowAt indexPath:IndexPath) - >的UITableViewCell { 讓細胞= demoTableVIew.dequeueReusableCell?(withIdentifier: 「小區」,爲:indexPath) cell.textLabel的.text =「CELL NUMBER:\(indexPath.row)「 return cell } –

相關問題