Невозможно создать фрейм для UITableView - PullRequest
0 голосов
/ 21 января 2019

Я создал 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 голосов
/ 21 января 2019

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

}

...