【问题标题】:Add UIView and UITableView programmatically以编程方式添加 UIView 和 UITableView
【发布时间】:2019-07-25 04:34:03
【问题描述】:

我正在尝试制作这个,如第一张图片所示:

Image 1

Image 2

但是视图并没有像我想要的那样显示出来。在这里你可以看到我的完整项目代码:

import UIKit
import RealmSwift
import CVCalendar

class Test: UIViewController, UITableViewDelegate, UITableViewDataSource {

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

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = UIColor(red: 4/255, green: 4/255, blue: 4/255, alpha: 1.0)
        self.navigationController?.navigationBar.barStyle = .black
        self.navigationController?.navigationBar.tintColor = UIColor.white
        self.navigationItem.title = "Test"
        self.navigationController?.navigationBar.prefersLargeTitles = true
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        // Get main screen bounds
        let screenSize: CGRect = UIScreen.main.bounds
        let screenWidth = screenSize.width
        let screenHeight = screenSize.height

        myView.frame = CGRect(x: 0, y: 0, width: screenWidth, height: 150)
        myView.backgroundColor = .black
        self.view.addSubview(myView)



        myTableView.frame = CGRect(x: 0, y: 50, width: screenWidth, height: screenHeight-50);
        myTableView.dataSource = self
        myTableView.delegate = self
        myTableView.backgroundColor = .blue
        myTableView.layer.borderWidth = 3

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

        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: "cell", for: indexPath)

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

        return cell
    }

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

为什么不显示?第一张是我想要的样子,第二张是现在的样子。

【问题讨论】:

  • 首先设置myView.backgroundColor = .black 并删除关于myTableView 的代码。构建并运行。黑色矩形出现了吗?
  • 另外(对不起,不是要锤你)请说明myView是如何声明/配置的。
  • @matt - 是的,黑色矩形确实出现在左上角。
  • 这就是我们期望发生的事情。到目前为止一切顺利!
  • 现在添加表格视图代码和myTableView.backgroundColor = .blue。也许还有myTableView.layer.borderWidth = 3。构建并运行。你看到表格视图了吗?

标签: ios swift xcode uitableview uiview


【解决方案1】:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

        if self.itemsToLoad[indexPath.row] != nil {
              cell.textLabel?.text = self.itemsToLoad[indexPath.row]
              cell.backgroundColor = .white

         }else {
               cell.backgroundColor = .blue

            }

        return cell
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-08
    • 2012-10-03
    • 1970-01-01
    • 1970-01-01
    • 2021-09-27
    • 2011-10-20
    相关资源
    最近更新 更多