UITableView создается программно в ViewController - PullRequest
0 голосов
/ 12 октября 2019

У меня трудности с созданием табличного представления программно в ViewController. У меня есть этот код:

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] Создание соединения клиент / демон: CEC5AA51-ECC7-4F7D-A540-A850CB470703 2019-10-12 10: 43: 15.760091 + 0200 My TableView [3922: 2655506] Получен ответ на метаданные запроса для: com.apple.MobileAsset.MacinTalkVoiceAssets, response: 0 2019-10-12 10: 43: 15.762379 + 0200 My TableView [3922: 2655506] Потребляемое расширение 2019-10-12 10: 43: 15.768843 + 0200 My TableView [3922: 2655506] Получен ответ на метаданные запроса для: com.apple.MobileAsset.MacinTalkVoiceAssets, response: 0 2019-10-12 10: 43: 17.013095 + 0200 My TableView [3922: 2655311] [AXRuntimeCommon] Неизвестный клиент: My TableView

1 Ответ

0 голосов
/ 12 октября 2019

Вы не установили Bottom Anchor или Height Anchor для UITableView с именем myTV

. Напишите этот код в функции 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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...