5
votes

I have a storyboard which contains UIViewController which has UITableview and UITableViewCell, everything is hooked up properly and I have put all the required code for UIViewController.

When I run the app in the simulator one of the datasource function cellForRowAtIndexPath is never called but numberOfRowsInSection is called

  • "func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int" is called
  • But "func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell" IS NEVER CALLED

Below is my code for view controller:

import UIKit

class DVViewController: UIViewController, UITableViewDelegate, UITableViewDataSource
{

    @IBOutlet var tableView : UITableView?
    var tableData = ["Row 1", "Row 2", "Row 3"]
    let kCellID = "cell"

    override func viewDidLoad()
    {
        super.viewDidLoad()

        self.tableView?.delegate = self;
        self.tableView?.dataSource = self;

        //load cell
        self.tableView?.registerClass(UITableViewCell.self, forCellReuseIdentifier: kCellID);
        self.tableView?.reloadData();
    }

    //MARK: Tableview delegates
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return self.tableData.count;
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
       //var cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: kCellID)
        let cell : UITableViewCell = UITableViewCell (style: UITableViewCellStyle.Default, reuseIdentifier: kCellID);
        return cell;
    }
}

Can someone suggest why cellForRowAtIndexPath is never called?

3
Have you breakpointed and ensured that nothing is being released by ARC? At this point in time, you cannot rely on anything to be correct with Swift and the new Xcode Beta. Memory retaining, outlets, optionals have been changing with each update. Swift is a rapidly changing language, and frankly it's pretty silly asking people on Stack Overflow these questions until Swift is out of Beta. Try the same logic in Objective-C, if it works, you know where you're problem is.TheCodingArt
I recreated the project with objective c and still the have the same issue, "numberOfRowsInSection" is called but not the "cellForRowAtIndexPath".DP2
If all you're outlets are connected in Objective C, and the code matches that above, and there's no weird state that you're not displaying where the tableView could be set to nil, and tableData is returning the correct count, then you may want to test this using Xcode 5 and see the results. If this indeed works using Objective-C in Xcode 5 and not 6 you've confirmed it's an Xcode 6 problem. If not, it's your code.TheCodingArt
TheGamingArt, Will try that tomorrow and will update, thanks.DP2
Did you, by any chance, start this project as a "Master Detail" project, and then replace a controller that was derived from a UITableViewController with your current one, that isn't? (If you go have a look at the properties of your view controller in Interface Builder, what type does it say it is under the Quick Help tab in the inspector?)Matt Gibson

3 Answers

3
votes

Here is what I did, uninstalled Xcode 5 & 6 both the reinstalled Xcode 6 only, Ran the project and it WORKED. After 2 days of debugging its the reinstall which did the trick.

2
votes

I have just checked your code, it works.
Check to see whether or not numberOfRowsInSection is called. if not check the connection in storyboard.

0
votes

Try this:

class MainViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{

func viewDidLoad(){

super.viewDidLoad()

tableView.dataSource = self

tableView.delegate = self

}

}