Ajouter des Section dans un UITableView

#21

J’essaye toujours. J’arrive à créer mes section mais pas à les afficher sans les heures.

J’aimerai que chaque section à comme titre le jours, et en dessous les heures de chaque jours.

import UIKit
import Alamofire

struct WeatherProData {
    
    let date: String // must be `Date` for smart grouping
//    let weatherCode: String
//    let temp: Double
//    let cloud: Doublea
//    let rTemp: Double
//    let cloudBase: Double
//    let dewp: Double
//    let press: Double
//    let hydro: Double
//    let vent: Double
//    let rain: Double
//    let iso0: Double
//    let freezeRain: Double
//    let noSnow: Double
//    let snowUp: Double
//    let visibility: Double
//    let snowProb: Double
}

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
    @IBOutlet weak var tableView: UITableView!
    
    @objc func refresh(_ sender: Any) {
        // Call webservice here after reload tableview.
        tableView.reloadData()
        refreshControl.endRefreshing()
    }
    
    var data = [WeatherProData]()
    var sections = [String]()
    var dateSection = [[WeatherProData]]()
    var refreshControl = UIRefreshControl()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.delegate = self
        tableView.dataSource = self
        
//        RefrechControl sur UITableVIew
        refreshControl.attributedTitle = NSAttributedString(string: "Tirez pour rafraichir")
        refreshControl.addTarget(self, action: #selector(refresh), for: .valueChanged)
        self.tableView.addSubview(refreshControl)
        
        self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        
        
        var headers : HTTPHeaders = ["Content-Type":"application.json"]
        
        //        Ajout lofin et mot de passe pour la connection à weather pro
        let user = loginWeatherPro
        let password = motDePasseWeatherPro
        
        //        Ajout de l'autorisation de connection
        if let authorizationHeader = Request.authorizationHeader(user: user, password: password) {
            headers[authorizationHeader.key] = authorizationHeader.value
        }
        
        //        formattage de l'heure
        let now = Date()
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
        formatter.calendar = Calendar(identifier: .iso8601)
        formatter.timeZone = TimeZone.init(abbreviation: "UTC")
        formatter.locale = Locale(identifier: "en_US_POSIX")
        print(formatter.string(from: now))
        let days = Calendar.current.date(byAdding: .day, value: 1, to: now)
        
        let french = DateFormatter()
        french.dateStyle = .full
        french.dateFormat = "dd MMMM"
        french.locale = Locale(identifier: "FR_fr")
        print(french.string(from: now))
        
        //        preparation de l'url de base
        let urlB = urlDeBaseWeatherPro
        let locate = "locatedAt=3.284752,50.644164"
        let period = "&validPeriod=PT0S"
        let validFrom = "&validFrom=\(formatter.string(from: now))"
        let validUntil = "&validUntil=\(formatter.string(from: days!))"
        let fields = "&fields=" + fieldsParameter
        let url = urlB + locate + period + validFrom + validUntil + fields
        
        Alamofire.request(url, headers:headers).responseJSON { response in
            if let JSON = response.result.value as? [String: Any] {
                if let forecast = JSON ["forecasts"] as? NSArray {
                    for element in forecast {
                        if let dict = element as? [String: Any],
                            let dates = dict ["validFrom"] as? String {
                            self.data.append(WeatherProData(date: dates))
                            self.sections = Array(Set(self.data.map({ (WeatherProData) -> String in
                                WeatherProData.date
                            })))
                            self.sections.forEach({ (string) in self.dateSection.append([])})
                            for index in 0..<self.sections.count {
                                self.data.forEach({ (data) in
                                    if data.date == self.sections[index] {
                                        self.dateSection[index].append(data)
                                    }
                                })
                            }
                            self.tableView.reloadData()
                        }
                    }
                }
            }
        }
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return sections.count
    }
    
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return sections[section]
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        for index in 0..<sections.count {
            if index == section {
                return dateSection[index].count
            }
        }
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
//        let datas = data[indexPath.row]
        cell.textLabel?.text = dateSection[indexPath.section] [indexPath.row].date
//        cell.textLabel?.text = datas.date
        self.tableView.addSubview(self.refreshControl)
        return cell
        
    }
}

Voici ce que j’obtiens.

#22

Hello,

En fait tu enregistres dans ton tableau de sessions la donnée retournée par le webservice “WeatherProData.date”
Cette donnée est au format “Date” complet et il faut que tu la formate pour garder uniquement les informations que tu veux.

Essai ça:

let dateFormatterPrint = DateFormatter()
dateFormatter.dateFormat = "dd/MM/yyyy"

self.sections = Array(Set(self.data.map({ (WeatherProData) -> String in
       dateFormatter.string(from: fullDate) // output 14/01/2019
})))
#23

Merci, mais ces sections vont me rendre fous.

Pas moyen d’afficher la section avec la date du jour et afficher en dessous les infos par heure

import UIKit
import Alamofire

struct WeatherProData {
    
    let date: String // must be `Date` for smart grouping
//    let weatherCode: String
//    let temp: Double
//    let cloud: Doublea
//    let rTemp: Double
//    let cloudBase: Double
//    let dewp: Double
//    let press: Double
//    let hydro: Double
//    let vent: Double
//    let rain: Double
//    let iso0: Double
//    let freezeRain: Double
//    let noSnow: Double
//    let snowUp: Double
//    let visibility: Double
//    let snowProb: Double
}

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
    @IBOutlet weak var tableView: UITableView!
    
    @objc func refresh(_ sender: Any) {
        // Call webservice here after reload tableview.
        tableView.reloadData()
        refreshControl.endRefreshing()
    }
    
    var data = [WeatherProData]()
    var sections = [String]()
    var dateSection = [[WeatherProData]]()
    var refreshControl = UIRefreshControl()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.delegate = self
        tableView.dataSource = self
        
//        RefrechControl sur UITableVIew
        refreshControl.attributedTitle = NSAttributedString(string: "Tirez pour rafraichir")
        refreshControl.addTarget(self, action: #selector(refresh), for: .valueChanged)
        self.tableView.addSubview(refreshControl)
        
        self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        
        
        var headers : HTTPHeaders = ["Content-Type":"application.json"]
        
        //        Ajout lofin et mot de passe pour la connection à weather pro
        let user = loginWeatherPro
        let password = motDePasseWeatherPro
        
        //        Ajout de l'autorisation de connection
        if let authorizationHeader = Request.authorizationHeader(user: user, password: password) {
            headers[authorizationHeader.key] = authorizationHeader.value
        }
        
        //        formattage de l'heure
        let now = Date()
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
        formatter.calendar = Calendar(identifier: .iso8601)
        formatter.timeZone = TimeZone.init(abbreviation: "UTC")
        formatter.locale = Locale(identifier: "en_US_POSIX")
        print(formatter.string(from: now))
        let days = Calendar.current.date(byAdding: .day, value: 1, to: now)
        
        let french = DateFormatter()
        french.dateStyle = .full
        french.dateFormat = "dd MMMM"
        french.locale = Locale(identifier: "FR_fr")
        
        //        preparation de l'url de base
        let urlB = urlDeBaseWeatherPro
        let locate = "locatedAt=3.284752,50.644164"
        let period = "&validPeriod=PT0S"
        let validFrom = "&validFrom=\(formatter.string(from: now))"
        let validUntil = "&validUntil=\(formatter.string(from: days!))"
        let fields = "&fields=" + fieldsParameter
        let url = urlB + locate + period + validFrom + validUntil + fields
        
        Alamofire.request(url, headers:headers).responseJSON { response in
            if let JSON = response.result.value as? [String: Any] {
                if let forecast = JSON ["forecasts"] as? NSArray {
                    for element in forecast {
                        if let dict = element as? [String: Any],
                            let dates = dict ["validFrom"] as? String {
                            self.data.append(WeatherProData(date: dates))
                            self.sections = Array(Set(self.data.map({ (WeatherProData) -> String in
                                french.string(from: ???)
                            })))
                            self.sections.forEach({ (string) in self.dateSection.append([])})
                            for index in 0..<self.sections.count {
                                self.data.forEach({ (data) in
                                    if data.date == self.sections[index] {
                                        self.dateSection[index].append(data)
                                    }
                                })
                            }
                            self.tableView.reloadData()
                        }
                    }
                }
            }
        }
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return sections.count
    }
    
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return sections[section]
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        for index in 0..<sections.count {
            if index == section {
                return dateSection[index].count
            }
        }
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
//        let datas = data[indexPath.row]
        cell.textLabel?.text = dateSection[indexPath.section] [indexPath.row].date
//        cell.textLabel?.text = datas.date
        self.tableView.addSubview(self.refreshControl)
        return cell
        
    }
}

Voici ce que le JSON donne comme info :

{
    "forecasts": [
        {
            "locatedAt": [
                3.284752,
                50.644165
            ],
            "stationTimeZoneName": "Europe/Brussels",
            "validFrom": "2019-01-21T21:00:00+01:00",
            "validUntil": "2019-01-21T21:00:00+01:00",
            "validPeriod": "PT0S",
            "airTemperatureInCelsius": -2.3,
            "dewPointTemperatureInCelsius": -4.3,
            "feelsLikeTemperatureInCelsius": -5.6,
            "airPressureAtSeaLevelInHectoPascal": 1019.4,
            "windSpeedInKilometerPerHour": 8.5,
            "windDirectionInDegree": 186,
            "effectiveCloudCoverInPercent": 11,
            "totalCloudCoverInOcta": 3.8,
            "cloudCoverLowerThan2000MeterInOcta": 0,
            "cloudCoverLowerThan5000MeterInOcta": 0,
            "cloudBaseHeightInMeter": -1,
            "relativeHumidityInPercent": 86,
            "freezingLevelHeightInMeter": 0,
            "freezingRainProbabilityInPercent": 0,
            "noSnowPossibleBelowHeightInMeter": 0,
            "snowCertainAboveHeightInMeter": 8.7,
            "snowfallProbabilityInPercent": 0,
            "visibilityInMeter": 8461,
            "clearSkyUVIndex": 0,
            "uvIndexWithClouds": 0,
            "convectivePrecipitationProbabilityInPercent": 0,
            "precipitationProbabilityInPercent": 0,
            "precipitationType": 71,
            "hailProbabilityInPercent": 0,
            "thunderstormProbabilityInPercent": 0,
            "weatherCode": 1,
            "weatherCodeTraditional": 0
        },
        {
            "locatedAt": [
                3.284752,
                50.644165
            ],
            "stationTimeZoneName": "Europe/Brussels",
            "validFrom": "2019-01-21T22:00:00+01:00",
            "validUntil": "2019-01-21T22:00:00+01:00",
            "validPeriod": "PT0S",
            "airTemperatureInCelsius": -2.5,
            "dewPointTemperatureInCelsius": -4.4,
            "feelsLikeTemperatureInCelsius": -6.4,
            "airPressureAtSeaLevelInHectoPascal": 1018.7,
            "windSpeedInKilometerPerHour": 10.4,
            "windDirectionInDegree": 182,
            "effectiveCloudCoverInPercent": 15,
            "totalCloudCoverInOcta": 4.5,
            "cloudCoverLowerThan2000MeterInOcta": 0,
            "cloudCoverLowerThan5000MeterInOcta": 0.3,
            "cloudBaseHeightInMeter": 1398,
            "relativeHumidityInPercent": 87,
            "freezingLevelHeightInMeter": 0,
            "freezingRainProbabilityInPercent": 0,
            "noSnowPossibleBelowHeightInMeter": 0,
            "snowCertainAboveHeightInMeter": 0,
            "snowfallProbabilityInPercent": 1,
            "visibilityInMeter": 9411,
            "clearSkyUVIndex": 0,
            "uvIndexWithClouds": 0,
            "convectivePrecipitationProbabilityInPercent": 0,
            "precipitationProbabilityInPercent": 1,
            "precipitationType": 71,
            "hailProbabilityInPercent": 0,
            "thunderstormProbabilityInPercent": 0,
            "weatherCode": 1,
            "weatherCodeTraditional": 0
        },
        {
            "locatedAt": [
                3.284752,
                50.644165
            ],
            "stationTimeZoneName": "Europe/Brussels",
            "validFrom": "2019-01-21T23:00:00+01:00",
            "validUntil": "2019-01-21T23:00:00+01:00",
            "validPeriod": "PT0S",
            "airTemperatureInCelsius": -2.5,
            "dewPointTemperatureInCelsius": -4.5,
            "feelsLikeTemperatureInCelsius": -6.7,
            "airPressureAtSeaLevelInHectoPascal": 1018,
            "windSpeedInKilometerPerHour": 11.7,
            "windDirectionInDegree": 180,
            "effectiveCloudCoverInPercent": 23,
            "totalCloudCoverInOcta": 5.1,
            "cloudCoverLowerThan2000MeterInOcta": 0,
            "cloudCoverLowerThan5000MeterInOcta": 0.6,
            "cloudBaseHeightInMeter": 2797,
            "relativeHumidityInPercent": 86,
            "freezingLevelHeightInMeter": 0,
            "freezingRainProbabilityInPercent": 0,
            "noSnowPossibleBelowHeightInMeter": 0,
            "snowCertainAboveHeightInMeter": 0,
            "snowfallProbabilityInPercent": 5,
            "visibilityInMeter": 10672,
            "clearSkyUVIndex": 0,
            "uvIndexWithClouds": 0,
            "convectivePrecipitationProbabilityInPercent": 1,
            "precipitationProbabilityInPercent": 5,
            "precipitationType": 71,
            "hailProbabilityInPercent": 0,
            "thunderstormProbabilityInPercent": 0,
            "weatherCode": 2,
            "weatherCodeTraditional": 0
        },
        {
            "locatedAt": [
                3.284752,
                50.644165
            ],
            "stationTimeZoneName": "Europe/Brussels",
            "validFrom": "2019-01-22T00:00:00+01:00",
            "validUntil": "2019-01-22T00:00:00+01:00",
            "validPeriod": "PT0S",
            "airTemperatureInCelsius": -2.5,
            "dewPointTemperatureInCelsius": -4.5,
            "feelsLikeTemperatureInCelsius": -7,
            "airPressureAtSeaLevelInHectoPascal": 1017,
            "windSpeedInKilometerPerHour": 12.8,
            "windDirectionInDegree": 179,
            "effectiveCloudCoverInPercent": 33,
            "totalCloudCoverInOcta": 5.5,
            "cloudCoverLowerThan2000MeterInOcta": 0,
            "cloudCoverLowerThan5000MeterInOcta": 1.1,
            "cloudBaseHeightInMeter": 6082,
            "relativeHumidityInPercent": 86,
            "freezingLevelHeightInMeter": 0,
            "freezingRainProbabilityInPercent": 0,
            "noSnowPossibleBelowHeightInMeter": 0,
            "snowCertainAboveHeightInMeter": 0,
            "snowfallProbabilityInPercent": 7,
            "visibilityInMeter": 11781,
            "clearSkyUVIndex": 0,
            "uvIndexWithClouds": 0,
            "convectivePrecipitationProbabilityInPercent": 1,
            "precipitationProbabilityInPercent": 7,
            "precipitationType": 71,
            "hailProbabilityInPercent": 0,
            "thunderstormProbabilityInPercent": 0,
            "weatherCode": 3,
            "weatherCodeTraditional": 0
        },
        {
            "locatedAt": [
                3.284752,
                50.644165
            ],
            "stationTimeZoneName": "Europe/Brussels",
            "validFrom": "2019-01-22T01:00:00+01:00",
            "validUntil": "2019-01-22T01:00:00+01:00",
            "validPeriod": "PT0S",
            "airTemperatureInCelsius": -2.5,
            "dewPointTemperatureInCelsius": -4.5,
            "feelsLikeTemperatureInCelsius": -7.2,
            "airPressureAtSeaLevelInHectoPascal": 1016,
            "windSpeedInKilometerPerHour": 13.7,
            "windDirectionInDegree": 179,
            "effectiveCloudCoverInPercent": 45,
            "totalCloudCoverInOcta": 5.9,
            "cloudCoverLowerThan2000MeterInOcta": 0,
            "cloudCoverLowerThan5000MeterInOcta": 2.1,
            "cloudBaseHeightInMeter": 6078,
            "relativeHumidityInPercent": 86,
            "freezingLevelHeightInMeter": 0,
            "freezingRainProbabilityInPercent": 0,
            "noSnowPossibleBelowHeightInMeter": 0,
            "snowCertainAboveHeightInMeter": 0,
            "snowfallProbabilityInPercent": 7,
            "visibilityInMeter": 12390,
            "clearSkyUVIndex": 0,
            "uvIndexWithClouds": 0,
            "convectivePrecipitationProbabilityInPercent": 1,
            "precipitationProbabilityInPercent": 8,
            "precipitationType": 71,
            "hailProbabilityInPercent": 0,
            "thunderstormProbabilityInPercent": 0,
            "weatherCode": 4,
            "weatherCodeTraditional": 0
        },
        {
            "locatedAt": [
                3.284752,
                50.644165
            ],
            "stationTimeZoneName": "Europe/Brussels",
            "validFrom": "2019-01-22T02:00:00+01:00",
            "validUntil": "2019-01-22T02:00:00+01:00",
            "validPeriod": "PT0S",
            "airTemperatureInCelsius": -2.4,
            "dewPointTemperatureInCelsius": -4.3,
            "feelsLikeTemperatureInCelsius": -7.4,
            "airPressureAtSeaLevelInHectoPascal": 1014.7,
            "windSpeedInKilometerPerHour": 15.2,
            "windDirectionInDegree": 179,
            "effectiveCloudCoverInPercent": 56,
            "totalCloudCoverInOcta": 6.3,
            "cloudCoverLowerThan2000MeterInOcta": 0,
            "cloudCoverLowerThan5000MeterInOcta": 3,
            "cloudBaseHeightInMeter": 6073,
            "relativeHumidityInPercent": 86,
            "freezingLevelHeightInMeter": 0,
            "freezingRainProbabilityInPercent": 0,
            "noSnowPossibleBelowHeightInMeter": 0,
            "snowCertainAboveHeightInMeter": 0,
            "snowfallProbabilityInPercent": 8,
            "visibilityInMeter": 12892,
            "clearSkyUVIndex": 0,
            "uvIndexWithClouds": 0,
            "convectivePrecipitationProbabilityInPercent": 1,
            "precipitationProbabilityInPercent": 8,
            "precipitationType": 71,
            "hailProbabilityInPercent": 0,
            "thunderstormProbabilityInPercent": 0,
            "weatherCode": 4,
            "weatherCodeTraditional": 0
        },
        {
            "locatedAt": [
                3.284752,
                50.644165
            ],
            "stationTimeZoneName": "Europe/Brussels",
            "validFrom": "2019-01-22T03:00:00+01:00",
            "validUntil": "2019-01-22T03:00:00+01:00",
            "validPeriod": "PT0S",
            "airTemperatureInCelsius": -2.1,
            "dewPointTemperatureInCelsius": -4.1,
            "feelsLikeTemperatureInCelsius": -7.3,
            "airPressureAtSeaLevelInHectoPascal": 1013.3,
            "windSpeedInKilometerPerHour": 16.7,
            "windDirectionInDegree": 178,
            "effectiveCloudCoverInPercent": 65,
            "totalCloudCoverInOcta": 6.6,
            "cloudCoverLowerThan2000MeterInOcta": 0,
            "cloudCoverLowerThan5000MeterInOcta": 3.7,
            "cloudBaseHeightInMeter": 4008,
            "relativeHumidityInPercent": 86,
            "freezingLevelHeightInMeter": 0,
            "freezingRainProbabilityInPercent": 0,
            "noSnowPossibleBelowHeightInMeter": 0,
            "snowCertainAboveHeightInMeter": 0,
            "snowfallProbabilityInPercent": 7,
            "visibilityInMeter": 13884,
            "clearSkyUVIndex": 0,
            "uvIndexWithClouds": 0,
            "convectivePrecipitationProbabilityInPercent": 1,
            "precipitationProbabilityInPercent": 7,
            "precipitationType": 71,
            "hailProbabilityInPercent": 0,
            "thunderstormProbabilityInPercent": 0,
            "weatherCode": 5,
            "weatherCodeTraditional": 0
        },
        {
            "locatedAt": [
                3.284752,
                50.644165
            ],
            "stationTimeZoneName": "Europe/Brussels",
            "validFrom": "2019-01-22T04:00:00+01:00",
            "validUntil": "2019-01-22T04:00:00+01:00",
            "validPeriod": "PT0S",
            "airTemperatureInCelsius": -1.9,
            "dewPointTemperatureInCelsius": -3.9,
            "feelsLikeTemperatureInCelsius": -7.3,
            "airPressureAtSeaLevelInHectoPascal": 1011.8,
            "windSpeedInKilometerPerHour": 18.1,
            "windDirectionInDegree": 177,
            "effectiveCloudCoverInPercent": 74,
            "totalCloudCoverInOcta": 6.9,
            "cloudCoverLowerThan2000MeterInOcta": 0.3,
            "cloudCoverLowerThan5000MeterInOcta": 4.5,
            "cloudBaseHeightInMeter": 4008,
            "relativeHumidityInPercent": 86,
            "freezingLevelHeightInMeter": 0,
            "freezingRainProbabilityInPercent": 0,
            "noSnowPossibleBelowHeightInMeter": 0,
            "snowCertainAboveHeightInMeter": 0,
            "snowfallProbabilityInPercent": 6,
            "visibilityInMeter": 14956,
            "clearSkyUVIndex": 0,
            "uvIndexWithClouds": 0,
            "convectivePrecipitationProbabilityInPercent": 1,
            "precipitationProbabilityInPercent": 6,
            "precipitationType": 71,
            "hailProbabilityInPercent": 0,
            "thunderstormProbabilityInPercent": 0,
            "weatherCode": 6,
            "weatherCodeTraditional": 0
        },
        {
            "locatedAt": [
                3.284752,
                50.644165
            ],
            "stationTimeZoneName": "Europe/Brussels",
            "validFrom": "2019-01-22T05:00:00+01:00",
            "validUntil": "2019-01-22T05:00:00+01:00",
            "validPeriod": "PT0S",
            "airTemperatureInCelsius": -2,
            "dewPointTemperatureInCelsius": -3.8,
            "feelsLikeTemperatureInCelsius": -7.8,
            "airPressureAtSeaLevelInHectoPascal": 1010.4,
            "windSpeedInKilometerPerHour": 20.4,
            "windDirectionInDegree": 177,
            "effectiveCloudCoverInPercent": 76,
            "totalCloudCoverInOcta": 7,
            "cloudCoverLowerThan2000MeterInOcta": 0.6,
            "cloudCoverLowerThan5000MeterInOcta": 4.6,
            "cloudBaseHeightInMeter": 5411,
            "relativeHumidityInPercent": 88,
            "freezingLevelHeightInMeter": 0,
            "freezingRainProbabilityInPercent": 0,
            "noSnowPossibleBelowHeightInMeter": 0,
            "snowCertainAboveHeightInMeter": 0,
            "snowfallProbabilityInPercent": 9,
            "visibilityInMeter": 13442,
            "clearSkyUVIndex": 0,
            "uvIndexWithClouds": 0,
            "convectivePrecipitationProbabilityInPercent": 2,
            "precipitationProbabilityInPercent": 10,
            "precipitationType": 71,
            "hailProbabilityInPercent": 0,
            "thunderstormProbabilityInPercent": 0,
            "weatherCode": 6,
            "weatherCodeTraditional": 0
        },
        {
            "locatedAt": [
                3.284752,
                50.644165
            ],
            "stationTimeZoneName": "Europe/Brussels",
            "validFrom": "2019-01-22T06:00:00+01:00",
            "validUntil": "2019-01-22T06:00:00+01:00",
            "validPeriod": "PT0S",
            "airTemperatureInCelsius": -1.8,
            "dewPointTemperatureInCelsius": -3.6,
            "feelsLikeTemperatureInCelsius": -7.7,
            "airPressureAtSeaLevelInHectoPascal": 1008.9,
            "windSpeedInKilometerPerHour": 21.3,
            "windDirectionInDegree": 176,
            "effectiveCloudCoverInPercent": 83,
            "totalCloudCoverInOcta": 7.1,
            "cloudCoverLowerThan2000MeterInOcta": 1.6,
            "cloudCoverLowerThan5000MeterInOcta": 5.4,
            "cloudBaseHeightInMeter": 1588,
            "relativeHumidityInPercent": 87,
            "freezingLevelHeightInMeter": 722,
            "freezingRainProbabilityInPercent": 1,
            "noSnowPossibleBelowHeightInMeter": 0,
            "snowCertainAboveHeightInMeter": 0,
            "snowfallProbabilityInPercent": 13,
            "visibilityInMeter": 13463,
            "clearSkyUVIndex": 0,
            "uvIndexWithClouds": 0,
            "convectivePrecipitationProbabilityInPercent": 4,
            "precipitationProbabilityInPercent": 15,
            "precipitationType": 71,
            "hailProbabilityInPercent": 0,
            "thunderstormProbabilityInPercent": 0,
            "weatherCode": 7,
            "weatherCodeTraditional": 0
        },
        {
            "locatedAt": [
                3.284752,
                50.644165
            ],
            "stationTimeZoneName": "Europe/Brussels",
            "validFrom": "2019-01-22T07:00:00+01:00",
            "validUntil": "2019-01-22T07:00:00+01:00",
            "validPeriod": "PT0S",
            "airTemperatureInCelsius": -1.3,
            "dewPointTemperatureInCelsius": -3.2,
            "feelsLikeTemperatureInCelsius": -7.1,
            "airPressureAtSeaLevelInHectoPascal": 1007.7,
            "windSpeedInKilometerPerHour": 21.5,
            "windDirectionInDegree": 175,
            "effectiveCloudCoverInPercent": 95,
            "totalCloudCoverInOcta": 7.7,
            "cloudCoverLowerThan2000MeterInOcta": 5.2,
            "cloudCoverLowerThan5000MeterInOcta": 7.1,
            "cloudBaseHeightInMeter": 1457,
            "relativeHumidityInPercent": 87,
            "freezingLevelHeightInMeter": 749.1,
            "freezingRainProbabilityInPercent": 1,
            "noSnowPossibleBelowHeightInMeter": 0,
            "snowCertainAboveHeightInMeter": 0,
            "snowfallProbabilityInPercent": 19,
            "visibilityInMeter": 14784,
            "clearSkyUVIndex": 0,
            "uvIndexWithClouds": 0,
            "convectivePrecipitationProbabilityInPercent": 7,
            "precipitationProbabilityInPercent": 22,
            "precipitationType": 71,
            "hailProbabilityInPercent": 0,
            "thunderstormProbabilityInPercent": 0,
            "weatherCode": 8,
            "weatherCodeTraditional": 0
        }
    ]
}

Merci

#24

Hello,

Qu’est-ce que tu obtiens comme display sur ton app ?

#25

Hello,

Sur je met ceci :

 self.sections = Array(Set(self.data.map({ (WeatherProData) -> String in
                                french.string(from: ????)
                            })))

forcement je n’arrive pas à compiler car from : ??? je ne voie pas quoi mettre !

mais si je fait comme avant que s0ta m’explique j’ai ceci :

self.sections = Array(Set(self.data.map({ (WeatherProData) -> String in
                                WeatherProData.date
                            })))

#26

Hello,

Il faut que tu mettes

self.sections = Array(Set(self.data.map({ (WeatherProData) -> String in
                            french.string(from: WeatherProData.date)
                        })))

J’imagine que WeatherProData.date te retourne un objet au format Date. Si ce n’est pas le cas, tu dois le convertir au format Date au moment de le recevoir dans ta closure.

#27

C’est ce que j’ai fait mais j’ai une erreur

Unable to infer closure type in the current context

en me soulignant en rouge le { après self.data.map ({

#28

Et donc si tu fais

self.sections = Array(Set(self.data.map({ (WeatherProData) -> String in
                                WeatherProData.date
                            })))

Tu obtiens l’écran du dessus ?

Si c’est le cas, ça veut dire que ce n’est peut-être pas un objet Date que tu reçois de WeatherProData.
Si dans la closure tu fais let date = WeatherProData.date et qu’à la ligne suivante tu tapes juste date, il devrait te mettre le type de variable que ça génère. Est-ce que c’est une variable de type Date ?

#29

Effectivement Date et de type String depuis WeatherProData
Car les données du JSON sont de type String

#30

Donc c’est pour ça que tu ne peux pas les convertir en String car french.string attend une variable de type Date.

Tu peux donc faire

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssXXX"
let weatherProDataDate = dateFormatter.date(from: WeatherProData.date)
french.string(from: weatherProDataDate)

Et là, logiquement, tu devrais avoir les dates dans le format french que tu as défini.

#31

J’ai une erreur Instance member ‘date’ cannot be used on type ‘WeatherProData’

Je commence à ne plus rien y comprendre

#32

Initialise le peut-être avant :

let jsonDateString = WeatherProData.date
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssXXX"
let weatherProDataDate = dateFormatter.date(from: jsonDateString)
french.string(from: weatherProDataDate)
#33

J’ai essayer de l’initialiser mais à chaque fois que je met WeatherProData.date

il me propose WeatherProData.init(date: String )

:weary:

J’ai mis le petit projet sur Github https://github.com/superdevil669/TestAppli.git

#34

J’ai une erreur 404 sur ton lien GitHub :confused:

Je ne connais pas du tout WeatherPro et je n’utilise pas Alamofire donc je découvre un peu avec toi !

Et donc si tu fais par exemple

let test =  WeatherProData.init(date: WeatherProData.date)

test est une variable de type Date ?

#35

Non toujours de type String

Je met mon lien en public dés que possible car c’est une répertoire Privé pour le moment

Voila il et public : https://github.com/superdevil669/TestAppli.git

#36

C’est bon, je l’ai récupéré. Par contre, je ne peux pas le build car il y a des Pods en dépendance et ils ne sont pas dans le projet.

#37

C’est Alamofire j’ai rien d’autre comme pod
j’ai Ajouter

#38

Là, ça build :slight_smile:
Je regarde ça.

#39

J’ai les titres comme ça maintenant.

Et avec de la donnée dans les cellules

#40

Super :ok_hand: mais comment faire pour dans les données il soit juste affiché l’heure et surtout faire en sorte que chaque cellule soit dans la bonne section ?