Emi
Emi

Reputation: 5

Custom tableView not showing at all

I'm trying to program a custom table view and I'm having difficulties getting it to show at all. The rootViewController is set to HoursTableViewController. I tried changing the background color of the tableView to see if it would show, but it doesn't. I'm pretty lost at this point. I also get this error in the console but I'm not sure if this is relevant to my problem:

Unknown class _TtC12Voluntracker14ViewController in Interface Builder file.

Any help would be greatly appreciated! I tried to change attributes of the tableView suggested in other posts but it still doesn't show.

    class HoursTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var hourDonationSegmentedButton: UISegmentedControl!
    var addEntryButton: UIButton!
    var editEntryButton: UIButton!

    var hoursEntries = [String]()
    var hoursTableView: UITableView = HoursTableView()




    override func viewDidLoad() {
        super.viewDidLoad()
        hoursTableView.dataSource = self
        hoursTableView.delegate = self
        hoursTableView.register(HoursTableViewCell.self, forCellReuseIdentifier: "HoursTableViewCell")
        prepareTableView()
    }

    func prepareTableView() {
        view.addSubview(hoursTableView)
        hoursTableView.backgroundColor = .green
        hoursTableView.translatesAutoresizingMaskIntoConstraints = false
        hoursTableView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        hoursTableView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
        hoursTableView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
        hoursTableView.rowHeight = UITableView.automaticDimension
        hoursTableView.estimatedRowHeight = 44
        hoursTableView.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)
        hoursTableView.backgroundColor = .green
    }


}

extension HoursTableViewController {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1;

    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        var cell = hoursTableView.dequeueReusableCell(withIdentifier: "HoursTableViewCell", for: indexPath) as! HoursTableViewCell
        //let hoursEntry = hoursEntries[indexPath.row]
        cell.hourLabel.text = "6"
        cell.minuteLabel.text = "6"
        cell.dateLabel.text = "6"
        cell.organizationLabel.text = "6"
        //cell = makeCellFromHoursEntry(currentCell: cell, hoursEntry: hoursEntry)
        return cell
    }

Upvotes: 0

Views: 70

Answers (1)

Joshua
Joshua

Reputation: 2451

Putting it as an answer as it's becoming too long for a comment.

Are constraints asking to autolayout and manual sizing specifying the exact size? answer is Yes.

If you want to set the frame just remove translateAutoResizingMaskIntoConstraint or set it to translatesAutoresizingMaskIntoConstraints = true. Problem there will be your anchors will not be taken into consideration during layout.

if you want it to be fully autoLayout remove the frame and just set the bottom constraint as hoursTableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true

Upvotes: 1

Related Questions