简体   繁体   English

在滚动视图中看不到表格视图

[英]Cannot see table view in scroll view

I am trying to add a table view to my scroll view:我正在尝试将表格视图添加到我的滚动视图中:

class ViewController: UIViewController {
    
    let tableView = UITableView()

    override func viewDidLoad() {
        super.viewDidLoad()

        let scrollView = UIScrollView()
        scrollView.backgroundColor = .red

        
        view.addSubview(scrollView)
        scrollView.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            scrollView.topAnchor.constraint(equalTo: view.topAnchor),
            scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
        ])
        
        scrollView.addSubview(tableView)
        
        tableView.backgroundColor = .yellow
        tableView.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            tableView.topAnchor.constraint(equalTo: scrollView.topAnchor),
            tableView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor),
            tableView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor),
            tableView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor)
        ])
        
        scrollView.contentSize = CGSize(width: view.bounds.width, height: 2000)
    }


}

However it doesn't show.但是它没有显示。 It only shows the scrollview background color.它只显示滚动视图的背景颜色。 What am I doing wrong here?我在这里做错了什么?

Is that all of the code in your test?这就是您测试中的所有代码吗? You've not set the datasource and delegate of the tableview, and haven't returned any cells so it's not going to show anything.您尚未设置 tableview 的数据源和委托,也没有返回任何单元格,因此它不会显示任何内容。

The problem is the constraint for tableView in NSLayoutConstraint.问题是 NSLayoutConstraint 中 tableView 的约束。

tableView.topAnchor.constraint(equalTo: scrollView.topAnchor),
tableView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor),
tableView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor),
tableView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor)

You use view.bottomAnchor or anyothers instead of scrollView, such as:您使用 view.bottomAnchor 或任何其他代替 scrollView,例如:

tableView.topAnchor.constraint(equalTo: view.topAnchor),
tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor)

This will display your tableView.这将显示您的 tableView。 Now you can try to fight with other problems caused by put tableView inside scrollView.现在您可以尝试解决将 tableView 放入 scrollView 引起的其他问题。

Don't use Scrollview under Tableview , it will make scrollview useless .不要在 Tableview 下使用 Scrollview ,它会使 scrollview无用 Tableview has inbuilt scrollview(kind of) as you fill up the tables, if the tables row is many and go beyond your screen then scrollview in tableview is automatically enabled.当您填满表格时,Tableview 具有内置的滚动视图(种类),如果表格行很多并且 go 超出您的屏幕,则自动启用 tableview 中的滚动视图。 So,Use only tableview.所以,只使用表格视图。

Add heightAnchor constraint to your table view to fix this issue.heightAnchor约束添加到您的表视图以解决此问题。 For example:例如:

tableView.heightAnchor.constraint(equalToConstant: 4 * 80)

This example add height anchor to the table view (4 is number of rows, 80 is height of a row)此示例将高度锚点添加到表视图(4 是行数,80 是行高)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM