Reputation: 11
I am creating this "weather app" I am new to swift and I know even less about API's.
I think I was able to receive some data from the [1]: https://api.met.no/weatherapi/locationforecast/2.0/documentation%23!/data/get_compact#!/data/get_compact_format API, but it is shown in the console window while I am basically trying to make it appear in my UItableview cells.
Can anyone help?
This is my viewController.swift file:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
let data = ["Nå", "Neste time", "Neste 6 timer", "Neste 12 timer"]
let subtitles = ["Temperatur:", "Vær:", "Vær:", "Vær:"]
struct WeatherManager: Codable {
let air_temperature: Int
let next_1_hours: Int
let next_6_hours: Int
let next_12_hours: String
}
override func viewDidLoad() {
super.viewDidLoad()
metAPI.shared.fetchWeatherManager()
tableView.delegate = self
tableView.dataSource = self
}
}
extension ViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("You tapped me")
}
}
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "firstCell", for: indexPath)
cell.textLabel?.text = data[indexPath.row]
cell.detailTextLabel?.text = subtitles[indexPath.row]
return cell
}
}
and this is my weatherManager.swift file:
final class metAPI {
static let shared = metAPI()
func fetchWeatherManager() {
let url = URL(string: "https://api.met.no/weatherapi/locationforecast/2.0/compact.json?lat=10.74481&lon=59.91116")!
let task = URLSession.shared.dataTask(with: url) {(data, response, error) in
guard let data = data else { return }
print(String(data: data, encoding: .utf8)!)
}
task.resume()
/*let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
guard let data = data else {
print("data was nil")
return
}
guard (try? JSONDecoder().decode(WeatherManager.self, from: data)) != nil else {
print("couldnt decode json")
return
}
}
task.resume()
}*/
}
struct WeatherManager: Codable {
let air_temperature: Int
let next_1_hours: Int
let next_6_hours: Int
let next_12_hours: String
}
}
I want that different data to be displayed in the right side of each cell, so:
air_temperature: Int, 10 degrees
next_1_hours: Int, Rain 4mm
next_6_hours: Int, Cloudy 3mm
next_12_hours: String, SUN
If anyone could help me out a little? :)
Upvotes: 0
Views: 76
Reputation: 27
If you want to display all of that data in your cell, you won't be able to use the default cells, at least if you want to do it elegantly. Create your own custom cell, with all the labels, icons/images you want and then you will be able to fill in the data in a much more intuitive way.
If you're using storyboard, you can use the cell it gives you in the tableview to model out your own custom one. Place your labels, imageviews, constraints, etc. and then create your outlets in a new swift file, then instead of:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "firstCell", for: indexPath)
cell.textLabel?.text = data[indexPath.row]
cell.detailTextLabel?.text = subtitles[indexPath.row]
return cell
}
you will add the name of the class you created for the cell as the return type for cellForRowAt like so:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> {customCellNameHere} {
let cell = tableView.dequeueReusableCell(withIdentifier: "firstCell", for: indexPath)
{customCellNameHere}.{whateverYouNamedItInTheOutlet}.text = data[indexPath.row]
{customCellNameHere}.{whateverYouNamedItInTheOutlet}.text = subtitles[indexPath.row]
return cell
}
Upvotes: 0
Reputation: 2555
That default UITableViewCell you are using is pretty limited in the design and placement of the items.
To have more control of the type of items and their placement on each cell, your best bet would be to create a custom UITableViewCell and layout your UILabel(s) exactly where you want them.
Here's an example: https://www.ralfebert.de/ios-examples/uikit/uitableviewcontroller/custom-cells/
Upvotes: 0