首页 > 解决方案 > Implementing TableView in ScrollView doesn't show the table

问题描述

haven't spent a day without a stuck. I was trying to implement TableView in a ScrollView, but it never showed up.

The code is here:

@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
    super.viewDidLoad()

    tableView.delegate = self
    tableView.dataSource = self
    tableView.register(UITableViewCell.self, forCellReuseIdentifier: "logTableCell")
}



var logs: [String] = ["Data1", "Data12", "Data13", "Data14"]

extension ViewController: UITableViewDelegate, UITableViewDataSource {

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

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

    cell.textLabel?.text = logs[indexPath.row]
    print("I am at table place")
    return cell
}   

}

And also a screen of StoryBoard StoryBoard

I checked the id of cell, tried to connect a tableView again, read/watched instructions how to do that, but it seems I am still doing something wrong. P.S. nothing is being printed in console from print() statement

标签: iosswiftuitableviewtableview

解决方案


由于tableView已经在堆栈视图中,请尝试将tableView- 的高度设置为 200。此外,尝试为 添加背景tableView,这样您就会知道 是否tableView占用了适量的空间。关于不调用委托方法,如果它tableView本身不在屏幕上,那么调用这些方法是没有意义的。所以我认为它不会被调用。


推荐阅读