dgelinas21
dgelinas21

Reputation: 641

table view is not loading data

I am running the following code, and have a UITableView nested within a UIViewController. The code should be using a nib to populate the rows, but for some reason nothing shows up when the UITableView loads. Below is the following class associated with the UIViewController that has an embedded tableview. Does anyone know why this may not be loading?

Code I have tried:

import UIKit
import Firebase

class Today: UIViewController, UITableViewDelegate, UITableViewDataSource  {

    var anArray: [String] = ["a", "b", "c"]

    @IBOutlet weak var myTableView: UITableView!
    override func viewDidLoad() {

        let nib = UINib.init(nibName: "ScheduleCellTableViewCell", bundle: nil)

        self.myTableView.registerNib(nib, forCellReuseIdentifier: "cell")

        self.myTableView.reloadData() //<<<<<<<<<<<<< RELOADS TABLEVIEW

    }//viewDidLoad() ends here


    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        //return the count of the number of groups
        return anArray.count
    }


    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! ScheduleCellTableViewCell

        let val = indexPath.row

        cell.testLabel.text = anArray[val]

        return cell
    }

    public func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 160.0
    }
}

Upvotes: 1

Views: 3742

Answers (4)

Irshad Ahmad
Irshad Ahmad

Reputation: 1383

Make sure your UITableview delegate and datasource assign properly from storyboard if not then assign them into your viewDidLoad function

override func viewDidLoad() {

        let nib = UINib.init(nibName: "ScheduleCellTableViewCell", bundle: nil)

        self.myTableView.registerNib(nib, forCellReuseIdentifier: "cell")
        self.myTableView.delegate = self
        self.myTableView.dataSource = self
        self.myTableView.reloadData() // <<<<<< RELOADS TABLEVIEW

    }

Upvotes: 1

shahana mh
shahana mh

Reputation: 67

//connect an outlet for table view

@IBOutlet var tableView: UITableView!
override func viewDidLoad() {

    self.tableView.delegate = self
    self.tableView.datasource = self
    self.tableView.reloadData()

}

Upvotes: 0

Rajamohan S
Rajamohan S

Reputation: 7269

You don't need to reloadData() for UITableView on viewDidLoad().

connect UITableView dataSource and delegate in UIViewController of the referencingOutlets. Like as below,

enter image description here

Upvotes: 5

Lawliet
Lawliet

Reputation: 3499

The issue could be in ScheduleCellTableViewCell, you have not set the CellIdentifier to be "cell".

Btw, I don't think self.myTableView.reloadData() is needed in viewDidLoad().

Upvotes: 0

Related Questions