无法为UITableView创建框架

时间:2019-01-21 09:12:38

标签: ios swift uitableview cgrect

我制作了一个tableView,它显然从屏幕的顶部开始,但是我想在除表头之外的顶部添加一个标签。

有人可以提供帮助吗?在此先感谢...。

我试图通过使用cgrect并为表提供框架来执行此操作,但是当我执行代码时,什么也没有发生,并且表仍然只是视图中存在的内容,而标签却无处可寻。

我还确保在表格后添加标签,以免重叠,但不起作用。

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)

1 个答案:

答案 0 :(得分:1)

enter image description here导入UIKit

import UIKit

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])")
}

}