2017-06-05 29 views
0

我在UITableViewController中創建表視圖時遇到問題。行數返回非零數字,並且在故事板中設置高度。但cellForRowAt從未被調用。swift - cellForRowAt從不叫

下面是代碼:

的ViewController:

import UIKit 

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 

@IBOutlet weak var tableView: UITableView! 


override func viewDidLoad() { 
    super.viewDidLoad() 

    // Do any additional setup after loading the view, typically from a nib. 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "articleCell", for: indexPath) as! ArticleCell 

    cell.title.text = "THIS IS A TESTE" 
    cell.desc.text = "THIS IS A TESTE" 

    return cell 
} 


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

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

有什麼不對我的代碼?

+2

您是否已將代表和數據源連接到控制器? –

回答

1

在您的viewDidLoad使

yourtableView.delegate = self 
yourtableView.dataSource = self 

不要忘了拉你的tableView的基準出口。 enter image description here

+0

我把這個代碼'self.tableView.register(ArticleCell.self,forCellReuseIdentifier:「articleCell」) tableView.delegate = self tableView.dataSource = self'但現在它給了我下面的錯誤:致命錯誤:意外地發現零同時展開可選值 – LMaker

+0

否,只需將這些行添加到ViewDidLoad中即可。 首先拉你tableview的參考插座。 –

+0

哇,它現在像一個魅力工作。參考在那裏,但我不知道發生了什麼。謝謝你,先生。 – LMaker