Ios 在ViewController中以编程方式创建的UITableView

Ios 在ViewController中以编程方式创建的UITableView,ios,swift,uitableview,Ios,Swift,Uitableview,我很难在ViewController中以编程方式创建tableView。我有以下代码: import UIKit class ViewController: UIViewController { let myTV = UITableView() var myNavBar: UINavigationBar! var arrayNumbers = [3, 42, 56, 55, 73] override func viewDidLoad() { super.viewDidLoad()

我很难在ViewController中以编程方式创建tableView。我有以下代码:

import UIKit

class ViewController: UIViewController {

let myTV = UITableView()
var myNavBar: UINavigationBar!
var arrayNumbers = [3, 42, 56, 55, 73]


override func viewDidLoad() {
    super.viewDidLoad()
    let margins = self.view.layoutMarginsGuide
    myNavBar = UINavigationBar()
    myNavBar.barStyle = .black
    myNavBar.translatesAutoresizingMaskIntoConstraints = false

    let myNavTitle = UINavigationItem(title: "Home")

    myNavBar.setItems([myNavTitle], animated: true)

    self.view.addSubview(myNavBar)

    myNavBar.leadingAnchor.constraint(equalTo: margins.leadingAnchor).isActive = true
    myNavBar.topAnchor.constraint(equalTo: margins.topAnchor, constant: 10).isActive = true
    myNavBar.trailingAnchor.constraint(equalTo: margins.trailingAnchor).isActive = true
    showContent()
}

func showContent() {

    if arrayNumbers.count > 0 {
        self.view.addSubview(myTV)
        myTV.backgroundColor = .clear
        myTV.separatorStyle = .none
        myTV.dataSource = self
        myTV.delegate = self
        myTV.register(UITableViewCell.self, forCellReuseIdentifier: "myCell")
        myTV.reloadData()
        myTV.translatesAutoresizingMaskIntoConstraints = false



        myTV.leadingAnchor.constraint(equalTo: myNavBar.leadingAnchor).isActive = true
        myTV.topAnchor.constraint(equalTo: myNavBar.bottomAnchor).isActive = true
        myTV.trailingAnchor.constraint(equalTo: myNavBar.trailingAnchor).isActive = true
    }
}


}


  extension ViewController: UITableViewDataSource {

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = myTV.dequeueReusableCell(withIdentifier: "myCell", for: indexPath)
    cell.textLabel?.text = "\(arrayNumbers[indexPath.row])"
    return cell
}
}


extension ViewController: UITableViewDelegate {

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print("You have selected \(arrayNumbers[indexPath.row]).")
}
}
在iPhone8的屏幕上,我只看到导航栏

在调试控制台上,我收到以下消息:

2019-10-12 10:43:15.730330+0200我的桌面视图[3922:2655506]正在创建 客户端/守护程序连接:CEC5AA51-ECC7-4F7D-A540-A850CB470703 2019-10-12 10:43:15.760091+0200我的TableView[3922:2655506]获得了 查询元数据回复:com.apple.MobileAsset.MacinTalkVoiceAssets, 回复:0 2019-10-12 10:43:15.762379+0200我的桌面视图[3922:2655506] 已消费分机2019-10-12 10:43:15.768843+0200车型年款 TableView[3922:2655506]获得了以下内容的查询元数据回复: com.apple.mobileaset.MacinTalkVoiceAssets,回复:0 2019-10-12 10:43:17.013095+0200我的表视图[3922:2655311][AXRuntimeCommon] 未知客户:我的TableView


您不能为名为
myTV的
UITableView
设置
Bottom锚定
Height锚定

showContent()函数中编写此代码并重试

    myTV.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
    myTV.topAnchor.constraint(equalTo: myNavBar.bottomAnchor).isActive = true
    myTV.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
    myTV.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true

您尚未为表视图设置底部约束,因此其高度将为0非常感谢!