表格内的 TableView Sheet 未正确显示 Swift

TableView inside Form Sheet not showing properly Swift

我正在尝试在表单中显示 TableView Sheet。这在 iPhone 上完全符合预期。但是,当我出于某种原因在 iPad 上测试它时,视图比表单 Sheet 大。 这是我正在谈论的图像:

iPad

如您所见,文本在框架中并非全部可见。全文为"THIS IS NOT SHOWING FULLY"

这是它应该的样子(忽略深色模式)

iPhone

在 MainController() 中,我通过在 ViewDidLoad() 中显示此页面 IntroController()

let navController = UINavigationController(rootViewController: IntroController())
present(navController, animated: true, completion: nil)

在 IntroController() 中,这是代码。

import UIKit

var tableview: UITableView!

class IntroController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: TableCell.reuseIdentifier()) as! TableCell
        cell.label.text = "Amount"
        cell.typeLabel.text = "THIS IS NOT SHOWING FULLY"
        cell.selectionStyle = .none
        cell.accessoryType = .none
        return cell
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        tableview = UITableView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height), style: .grouped)
        view.addSubview(tableview)

        tableview.register(UINib(nibName: TableCell.nibName(), bundle: nil), forCellReuseIdentifier: TableCell.reuseIdentifier())

        tableview.delegate = self
        tableview.dataSource = self

    }

}

有谁知道为什么会发生这种情况以及如何解决它?

谢谢!

不要将table视图作为子视图添加到VC的view中,而是直接将其设置为view:

view = tableview

或者,直接使用 UITableViewController。我强烈建议您选择这种方法。

class IntroController: UITableViewController {

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        ...
    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.register(UINib(nibName: TableCell.nibName(), bundle: nil), forCellReuseIdentifier: TableCell.reuseIdentifier())
    }

}

我不确定是什么原因造成的,但如果我猜的话,可能是因为 view.frame 出于某种原因没有设置为 [=28= 的内容大小的矩形] (尚未)调用 viewDidLoad 时。如果将添加 table 视图作为 view 的子视图的代码移动到 viewDidLayoutSubviews,那么它也可以工作:

override func viewDidLayoutSubviews() {
    if view.subviews.count < 1 {
        tableview = UITableView(frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: view.bounds.height), style: .grouped)
        tableview.register(UINib(nibName: TableCell.nibName(), bundle: nil), forCellReuseIdentifier: TableCell.reuseIdentifier())
        view.addSubview(tableview)

        tableview.delegate = self
        tableview.dataSource = self
    }
}