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

问题描述 投票:0回答:1

我很难在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]).")
}
}

在iPhone 8的屏幕上,我只看到导航栏。

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

2019-10-12 10:43:15.730330 + 0200我的TableView [3922:2655506]正在创建客户端/守护程序连接:CEC5AA51-ECC7-4F7D-A540-A850CB4707032019-10-12 10:43:15.760091 + 0200我的TableView [3922:2655506]查询元数据回复:com.apple.MobileAsset.MacinTalkVoiceAssets,响应:0 2019-10-12 10:43:15.762379 + 0200 My TableView [3922:2655506]消费了扩展2019-10-12 10:43:15.768843 + 0200我的TableView [3922:2655506]获得了以下查询的元数据答复:com.apple.MobileAsset.MacinTalkVoiceAssets,响应:0 2019-10-1210:43:17.013095 + 0200我的TableView [3922:2655311] [AXRuntimeCommon]未知的客户端:我的TableView

ios swift uitableview
1个回答
0
投票

您未将名为[[myTV的Bottom Anchor设置为Height AnchorUITableView]

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

© www.soinside.com 2019 - 2024. All rights reserved.