无法为 UITableView 创建框架

Cannot Create a frame for UITableView

我做了一个 tableView,它显然是从屏幕顶部开始的,但我想在顶部添加一个标签,而不是 table header。

有人可以提供帮助吗?提前致谢....

我试图通过使用 cgrect 并为 table 提供一个框架来做到这一点,但是当我执行代码时,没有任何反应并且 table 仍然是视图和标签上唯一存在的东西无处可寻。

我还确保在 table 之后添加标签,这样它就不会重叠但没有用。

class TableViewController: UITableViewController { 
 override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationController?.navigationBar.barTintColor = UIColor(red: 139/255, green: 26/255, blue: 137/255, alpha: 1.0)
    self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
    self.navigationController?.navigationBar.tintColor = .white
    self.navigationItem.backBarButtonItem?.title = "Back"
    checkForVolume()
    self.coachMarksController.dataSource = self        
    var tableFrame = self.tableView.frame;
    tableFrame.size.height = 200;
    self.tableView.frame = tableFrame;
    fileprivate var moduleTable: UITableView!
    moduleTable.frame = CGRect(x: 0, y: 100, width: 375 , height: 650)
    moduleTable = UITableView()
    moduleTable.delegate = self
    moduleTable.dataSource = self
    moduleTable.separatorStyle = .none
    moduleTable.allowsSelection = true
    moduleTable.rowHeight = view.frame.size.height * 0.11
    moduleTable.allowsMultipleSelection = false
    moduleTable.register(SettingCell.self, forCellReuseIdentifier: identifier)
    moduleTable.frame = CGRect(x: 0, y: 1500, width: 375 , height: 650)    view.addSubview(moduleTable)
    moduleTable.frame = CGRect(x: 0, y: 1500, width: 375 , height: 650)       
    let moduleLabel = UILabel()
    moduleLabel.text = "MODULES"
    moduleLabel.textAlignment = .center
    moduleLabel.textColor = .purple
    moduleLabel.frame = CGRect(x: 0, y: 60, width: 375, height:40)
    self.view.addSubview(moduleLabel)

导入 UIKit

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

var myTableView: UITableView  =   UITableView()
var itemsToLoad: [String] = ["One", "Two", "Three"]

override func viewDidLoad() {
    super.viewDidLoad()
    // Get main screen bounds
    let screenSize: CGRect = UIScreen.main.bounds

    let screenWidth = screenSize.width
    let screenHeight = screenSize.height

    myTableView.frame = CGRect(x: 50, y: 50, width: 132, height: 555)
    myTableView.dataSource = self
    myTableView.delegate = self

    myTableView.register(UITableViewCell.self, forCellReuseIdentifier: "myCell")

    self.view.addSubview(myTableView)    }



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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let cell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath as IndexPath)

    cell.textLabel?.text = self.itemsToLoad[indexPath.row]

    return cell
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath)
{
    print("User selected table row \(indexPath.row) and item \(itemsToLoad[indexPath.row])")
}

}