gpt4 book ai didi

ios - 在 ViewController 中以编程方式创建 UITableView

转载 作者:行者123 更新时间:2023-11-29 05:24:19 24 4
gpt4 key购买 nike

我在 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 My TableView[3922:2655506] Creating client/daemon connection: CEC5AA51-ECC7-4F7D-A540-A850CB470703 2019-10-12 10:43:15.760091+0200 My TableView[3922:2655506] Got the query meta data reply for: com.apple.MobileAsset.MacinTalkVoiceAssets, response: 0 2019-10-12 10:43:15.762379+0200 My TableView[3922:2655506] Consumed extension 2019-10-12 10:43:15.768843+0200 My TableView[3922:2655506] Got the query meta data reply for: com.apple.MobileAsset.MacinTalkVoiceAssets, response: 0 2019-10-12 10:43:17.013095+0200 My TableView[3922:2655311] [AXRuntimeCommon] Unknown client: My TableView

最佳答案

您没有为名为 myTVUITableView 设置Bottom AnchorHeight Anchor

在您的 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

关于ios - 在 ViewController 中以编程方式创建 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58352497/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com