2017-06-05 53 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