以编程方式添加UIView和UITableView

时间:2019-03-03 20:38:33

标签: ios swift xcode uitableview uiview

因此,我试图做到这一点,如第一张图片所示:

Image here Image 2 here

但是该视图以某种方式并没有像我想要的那样显示。在这里,您可以看到我的完整项目代码:

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

我不明白为什么它不会出现。有任何想法吗?第一张图片是我想要的,然后第二张图片是现在的样子。在此先感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

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
    }