UiTableview没有出现在屏幕上

时间:2017-08-23 20:28:02

标签: ios swift xcode uitableview

出于某种原因,我的tableview根本没有出现,即使每个其他元素都有。我在一个不同的项目中尝试了相同的代码,它完美地显示出来。

以下是我的tableview函数:

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    print("this will run")

    return 3
}

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

    cell.otsikko.text = "example"
    cell.kuvaus.text = "exampledesc"

    print("this code won't run")

    return cell
}

override func viewDidAppear(_ animated: Bool) {
    tableView.reloadData()
}

以及其他一些细胞设置:

class Cell : UITableViewCell {
    @IBOutlet weak var otsikko: UILabel!
    @IBOutlet weak var kuvaus: UITextView!
}

class HomeViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{
    @IBOutlet weak var tableView: UITableView!

故事板的图片:

Storyboard

运行应用程序的图像:

Run app

因此,正如您所看到的,它会跳过整个tableview并且不会生成单元格。它可能与布局和约束有关,但老实说我不知道​​。

1 个答案:

答案 0 :(得分:1)

我认为,第一步是检查你的viewcontroller for outlet,和 设置数据源和委托。

override func viewDidLoad {
   super.viewDidLoad()
   tableView.delegate = self
   tableView.datasource = self
}

enter image description here

相关问题