我制作了一个显然从屏幕顶部开始的 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)
最佳答案
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/