Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
415 views
in Technique[技术] by (71.8m points)

ios - Add UIView and UITableView programmatically

I am trying to make this, as in the first picture:

Image 1

Image 2

But the view somehow does not show up like I want it to. Here you can see my full project code:

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

Why won't it show up? The first picture is how I want it, then second picture is how it looks now.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
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
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...