Dipali Bajaj
Dipali Bajaj

Reputation: 67

TableView Content not showing up

I've created a UITableView inside my ViewController. Here's the code I've added to fill the TableView with content. However, no content is showing up. I've created a customized tableView Cell which I am calling in the cellNib as PostTableViewCell. When the program is running, it shows the prototype cells with the divider lines, but none of the custom content.

import Foundation
import UIKit
import Firebase

class HomeViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

var tableView:UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

    tableView = UITableView(frame: view.bounds, style: .plain)
    //tableView.backgroundColor = UIColor.blue

    let cellNib = UINib(nibName: "PostTableViewCell", bundle: nil)
    tableView.register(cellNib, forCellReuseIdentifier: "postCell")
    view.addSubview(tableView)

    var layoutGuide:UILayoutGuide!

    if #available(iOS 11.0, *) {
        layoutGuide = view.safeAreaLayoutGuide
    } else {
        layoutGuide = view.layoutMarginsGuide
    }

    tableView.leadingAnchor.constraint(equalTo: layoutGuide.leadingAnchor).isActive = true
    tableView.topAnchor.constraint(equalTo: layoutGuide.topAnchor).isActive = true
    tableView.trailingAnchor.constraint(equalTo: layoutGuide.trailingAnchor).isActive = true
    tableView.bottomAnchor.constraint(equalTo: layoutGuide.bottomAnchor).isActive = true

    tableView.delegate = self
    tableView.dataSource = self
    tableView.reloadData()
    // Do any additional setup after loading the view.
}

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 12
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "postCell", for: indexPath) as! PostTableViewCell
    return cell
}

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

/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/

}

Upvotes: 0

Views: 740

Answers (2)

Manish Mahajan
Manish Mahajan

Reputation: 2092

Code looks right.. Make sure you have assign content to table cell in cellForRowAt datasource methods.

Upvotes: 0

Craig
Craig

Reputation: 9330

You're not actually configuring the cell in your tableView:cellForRowAt: method:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "postCell", for: indexPath) as! PostTableViewCell
return cell
}

Typically you need to insert values into the elements of your custom cell. The "content" of your cell label text, images etc is usually inserted into the cells elements in this method. If you look at the code in a new Xcode project you will see:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)

    let object = objects[indexPath.row] as! NSDate
    cell.textLabel!.text = object.description
    return cell
}

Note the line retrieving the data: let object = objects[indexPath.row] as! NSDate

Followed by the line in setting the text value of the textLabel:

cell.textLabel!.text = object.description

Upvotes: 1

Related Questions