Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
420 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 Answer

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
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...