CellForRowAtIndexPath is never called
I have a storyboard that contains UIViewController
which has UITableview
and UITableViewCell
, everything is connected correctly, and I put all the necessary code for the UIViewController.
When I run the app in the simulator, one of the functions of the data source cellForRowAtIndexPath
is never numberOfRowsInSection
anumberOfRowsInSection
- "func tableView (tableView: UITableView, section numberOfRowsInSection: Int) → Int" is called
- But "func tableView (tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell" is NEVER CALLED
Below is my code for the 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 anyone suggest why it cellForRowAtIndexPath
never gets called?
source to share
I just checked your code, it works.
Check numberOfRowsInSection
whether numberOfRowsInSection
or not. if not, check the connection in the storyboard .