I am have been trying to solve this issue all day, but I just can't seem to find the problem. My code is not showing any errors, which in this case it might have been easier. The issue is that I can't get my custom table view cell to show when simulating the app. I have gone through this process before and there have never been this kind of problem. Outside of my view controller I have a table view controller and a table view cell controller (to customize the cell). I have linked the table view controller with my table view controller storyboard, and I have given the table cell an identifier. I also have another file (Forecast.swift) dedicated to parse the JSON.
Any idea of what I am doing wrong?
Below is my code from table view controller:
import UIKit
import Alamofire
import CoreLocation
class ForecastTableViewController: UITableViewController, CLLocationManagerDelegate {
let locationManager = CLLocationManager()
var currentLocation: CLLocation!
var forecast: Forecast!
var forecasts = [Forecast]()
func downloadForecastData(completed: DownloadComplete) {
Alamofire.request(FORECAST_URL, withMethod: .get, parameters: nil, encoding: .json).responseJSON { response in
let result = response.result
if let dict = result.value as? Dictionary<String, AnyObject> {
if let list = dict["list"] as? [Dictionary<String, AnyObject>] {
for obj in list {
let forecast = Forecast(weatherDict: obj)
self.forecasts.append(forecast)
print(obj)
}
self.forecasts.remove(at: 0)
self.tableView.reloadData()
}
}
completed()
}
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
self.tableView.register(ForecastCell.self, forCellReuseIdentifier: "forecastCell")
}
override func viewDidAppear(_ animated: Bool) {
locationAuthStatus()
}
func locationAuthStatus() {
if CLLocationManager.authorizationStatus() == .authorizedWhenInUse {
currentLocation = locationManager.location
Location.sharedInstance.latitude = currentLocation.coordinate.latitude
Location.sharedInstance.longitude = currentLocation.coordinate.longitude
self.downloadForecastData(completed: {})
} else {
locationManager.requestWhenInUseAuthorization()
locationAuthStatus()
}
}
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()
}
}
}And here is my code for the custom table cell:
import UIKit
class ForecastCell: UITableViewCell {
/
@IBOutlet weak var weatherTypeImage: UIImageView!
@IBOutlet weak var currentTempLabel: UILabel!
@IBOutlet weak var maxTempLabel: UILabel!
@IBOutlet weak var minTempLabel: UILabel!
@IBOutlet weak var weekdayLabel: UILabel!
@IBOutlet weak var weatherTypeLabel: UILabel!
func configureCell(forecast: Forecast) {
minTempLabel.text = "\(forecast.minTemp)"
maxTempLabel.text = "\(forecast.maxTemp)"
currentTempLabel.text = "\(forecast.forecastTemp)"
weatherTypeLabel.text = forecast.weatherType
weekdayLabel.text = forecast.date
weatherTypeImage.image = UIImage(named: forecast.weatherType)
}
}