ios - 无法为 UITableView 创建框架

标签 ios swift uitableview cgrect

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

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

我尝试通过使用 cgrect 并为表格提供一个框架来做到这一点,但是当我执行代码时,什么也没有发生,表格仍然只是 View 中存在的东西,标签无处可寻。

我还确保在表格后添加标签,这样它就不会重叠但不起作用。

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)

最佳答案

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

关于ios - 无法为 UITableView 创建框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54286592/

相关文章:

ios - xCode Sprite-Kit - 每个场景中的背景图像

Iphone 将 URL 加载到变量 - 静态有效,来自 XML 的动态失败

ios - UITextField becomeFirstResponder 在动态单元格中不起作用

objective-c - 如果 CoreLocation 必须不断检索用户的位置,节省电池的技巧?

Android 动态 View /布局 - 服务器端 View /布局生成

ios - Collection View /选项卡栏 Controller 崩溃

ios - 如何将后退图标添加到左侧栏按钮?

regex - swift 2 : How do I split a string without loosing the characters it split on?

ios - 更新到最新版本的 CocoaPods?

ios - 重用具有多个数据源的 UITableView 的最佳设计模式是什么?