Reputation: 129
I have this View Controller (see code below) linked to a table view controller storyboard. I get the actual table view showing but not my custom cell. Any ideas of what I am doing wrong?
Good to know: - I have a separate swift file specifically made for my cell - I have connected the cell with a cell identifier - This is not my main view controller.
Thanks in advance!
import UIKit
class ForecastTableViewController: UITableViewController {
var forecasts = [Forecast]()
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.reloadData()
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return forecasts.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "forecastCell", for: indexPath) as? ForecastCell {
let forecast = forecasts[indexPath.row]
cell.configureCell(forecast: forecast)
return cell
} else {
return ForecastCell()
}
}
}
Upvotes: 2
Views: 5074
Reputation: 835
Try to change your code in this way and remember to put your data inside your forecasts
array
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "forecastCell", for: indexPath) as? ForecastCell
let forecast = forecasts[indexPath.row]
cell.configureCell(forecast: forecast)
return cell
}
Upvotes: 0
Reputation: 6484
Put this inside your viewDidLoad()
method
self.tableView.registerNib(UINib(nibName: "ForecastCell", bundle: nil), forCellReuseIdentifier: "forecastCell")
Also make sure that you actually set the cell's identifier not restoration ID (common mistake)
Upvotes: 1
Reputation: 5060
Have you register your cell, if not register it first.
tableView.registerClass(MyCell.classForCoder(), forCellReuseIdentifier: kCellIdentifier)
OR if you are using cell with Xib then register
self.tableView.registerNib(UINib(nibName: "UICustomTableViewCell", bundle: nil), forCellReuseIdentifier: "UICustomTableViewCell")
Upvotes: 0
Reputation: 27438
Add below lines in viewDidload
above reload function,
self.tableView.delegate = self
self.tableView.dataSource = self
And no need of self.tableView.reloadData()
in viewDidload
so remove it!
Upvotes: 4