Reputation: 1133
Im having some trouble with populating my table view from my array. While my array is not empty (I tried printing it in the console and there are the data I wanted), the tableView is not populating with what I wanted but instead it is empty. I am wondering if I have done anything wrong here to cause that
Here's my code:
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return numberOfGamesPlayedRecently
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = UITableViewCell()
// display our ranked data
cell.textLabel?.text = "\(tableData[indexPath.row])"
return cell
}
where numberOfGamesPlayedRecently is an int and my tableData is a array of epoch time
Thanks!
Upvotes: 1
Views: 2133
Reputation: 51
I stumbled across your question while having the same issue myself. I was able to resolve my tableView not populating by:
1. setting the ViewController as a UITableViewDataSource in the class declaration and
2. in the ViewDidLoad method, setting the ViewController as the datasource like so: tableView.dataSource = self
Upvotes: 5
Reputation: 621
First off, as Caroline said, it's best practice to use a storyboard and use let cell = tableView.dequeueReusableCellWithIdentifier("CellIdentifier", forIndexPath: indexPath)
as! UITableViewCell
both to let you modify the cell in the storyboard and to greatly increase memory performance with large tables.
Second, I don't think the tables are connected. Your code all seems right, and it may be worthwhile to set some other property of the cell (like the background color), but it does seem like they aren't connected. Go to your storyboard, highlight the table view (not the view overall), open your property inspector, go to the "Custom Class" tab (third from the left, it looks like a small newspaper), and set the class property to the name of your custom UITableView class.
Upvotes: 0
Reputation: 4970
If you are using a storyboard, you should be using something like this to get a reference to the current cell:
let cell = tableView.dequeueReusableCellWithIdentifier("CellIdentifier", forIndexPath: indexPath)
as! UITableViewCell
Have you set your delegate and datasource? If you are using a UITableViewController you don't need to do that, but otherwise you do.
You should probably do tutorials to make sure you know how table views work.
There's one here: http://www.raywenderlich.com/81879/storyboards-tutorial-swift-part-1
Upvotes: 0