Good morning, everyone,
I have created a TableView that uses data from a JSON file.
I can put my data in my TableView with Alamofire, but for ease of reference I would like to sort them by section with date.
Because for each date over 15 days there are weather forecasts for each hour.
So ideally I would like a section per day that opens with the click and gives every hour with forecasts.
This is my ViewController:
import UIKit
import Alamofire
import MapKit
class WeatherProController: UIViewController, CLLocationManagerDelegate, UITableViewDataSource,UITableViewDelegate {
private let refreshControl = UIRefreshControl()
var datas = [WeatherProData]()
var locManager = CLLocationManager()
var currentLocation: CLLocation!
var timer = Timer()
var jour = UIColor(red: 0, green: 191 / 255, blue: 1, alpha: 1)
var nuit = UIColor(red: 51 / 255, green: 116 / 255, blue: 255 / 255, alpha: 1)
let didBecomeActiveNotificationName = UIApplication.didBecomeActiveNotification
let identifiantCell = "dataProCell"
let identifiantSegue = "versDetailDonneePro"
#IBOutlet weak var tableView: UITableView!
#IBOutlet weak var menuButton: UIBarButtonItem!
#IBOutlet weak var label: UILabel!
#IBOutlet weak var activityIndicatorView: UIActivityIndicatorView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
locManager.requestAlwaysAuthorization()
NotificationCenter.default.addObserver(self, selector: #selector(scheduleTimer), name: didBecomeActiveNotificationName, object: nil)
changeBackground()
data()
// Add Refresh Control to Table View
if #available(iOS 10.0, *) {
tableView.refreshControl = refreshControl
} else {
tableView.addSubview(refreshControl)
}
// Configure Refresh Control
refreshControl.addTarget(self, action: #selector(refreshWeatherData(_:)), for: .valueChanged)
refreshControl.tintColor = UIColor.white
let attributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
refreshControl.attributedTitle = NSAttributedString(string: "Refreshing please wait", attributes: attributes)
}
#objc private func refreshWeatherData(_ sender: Any) {
fetchWeatherData()
}
private func setupActivityIndicatorView() {
activityIndicatorView.startAnimating()
}
private func fetchWeatherData() {
data()
self.refreshControl.endRefreshing()
// self.activityIndicatorView.stopAnimating()
}
#objc func scheduleTimer() {
// schedule the timer
timer = Timer(fireAt: Calendar.current.nextDate(after: Date(), matching: DateComponents(hour: 6..<21 ~= Date().hour ? 21 : 6), matchingPolicy: .nextTime)!, interval: 0, target: self, selector: #selector(changeBackground), userInfo: nil, repeats: false)
print(timer.fireDate)
// RunLoop.main.add(timer, forMode: .RunLoop.Mode.common)
print("new background chenge scheduled at:", timer.fireDate.description(with: .current))
}
#objc func changeBackground(){
// check if day or night shift
self.view.backgroundColor = 6..<21 ~= Date().hour ? jour : nuit
// schedule the timer
scheduleTimer()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return datas.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: identifiantCell) as? WeatherProCell {
let data = datas[indexPath.row]
cell.creerCell(data)
return cell
}
return UITableViewCell()
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let cell = tableView.cellForRow(at: indexPath) as? WeatherProCell {
cell.textIsHidden.isHidden = !cell.textIsHidden.isHidden
cell.textIsHidden1.isHidden = !cell.textIsHidden1.isHidden
cell.textIsHidden2.isHidden = !cell.textIsHidden2.isHidden
cell.textIsHidden3.isHidden = !cell.textIsHidden3.isHidden
cell.textIsHidden4.isHidden = !cell.textIsHidden4.isHidden
cell.textIsHidden5.isHidden = !cell.textIsHidden5.isHidden
cell.textIsHidden6.isHidden = !cell.textIsHidden6.isHidden
cell.textIsHidden7.isHidden = !cell.textIsHidden7.isHidden
cell.textIsHidden8.isHidden = !cell.textIsHidden8.isHidden
cell.textIsHidden9.isHidden = !cell.textIsHidden9.isHidden
cell.cloud.isHidden = !cell.cloud.isHidden
cell.rTemp.isHidden = !cell.rTemp.isHidden
cell.cloudBase.isHidden = !cell.cloudBase.isHidden
cell.dewp.isHidden = !cell.dewp.isHidden
cell.press.isHidden = !cell.press.isHidden
cell.pressIcon.isHidden = !cell.pressIcon.isHidden
cell.hydro.isHidden = !cell.hydro.isHidden
cell.hydroIcon.isHidden = !cell.hydroIcon.isHidden
cell.vent.isHidden = !cell.vent.isHidden
cell.ventIcon.isHidden = !cell.ventIcon.isHidden
cell.rainIcon.isHidden = !cell.rainIcon.isHidden
cell.rain.isHidden = !cell.rain.isHidden
cell.iso0.isHidden = !cell.iso0.isHidden
cell.freezeRain.isHidden = !cell.freezeRain.isHidden
cell.noSnow.isHidden = !cell.noSnow.isHidden
cell.snowUp.isHidden = !cell.snowUp.isHidden
cell.visibility.isHidden = !cell.visibility.isHidden
cell.snowProb.isHidden = !cell.snowProb.isHidden
tableView.beginUpdates()
tableView.endUpdates()
tableView.deselectRow(at: indexPath, animated: true)
}
}
//func numberOfSections(in tableView: UITableView) -> Int {
// return datas.count
//}
//func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
// return datas[section].date
//}
func data() {
if ( CLLocationManager.authorizationStatus() == .authorizedWhenInUse ||
CLLocationManager.authorizationStatus() == .authorizedAlways) {
currentLocation = locManager.location
}
var headers: HTTPHeaders = [
"Content-Type": "application/json"
]
let user = loginWeatherPro
let password = motDePasseWeatherPro
if let authorizationHeader = Request.authorizationHeader(user: user, password: password) {
headers[authorizationHeader.key] = authorizationHeader.value
}
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: 15, to: now)
let urlB = urlDeBaseWeatherPro
let locate = "locatedAt=\(currentLocation.coordinate.longitude),\(currentLocation.coordinate.latitude)"
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
print(url)
Alamofire.request(url, headers:headers).responseJSON{ response in
if let JSON = response.result.value as? [String: AnyObject] {
if let forecast = JSON ["forecasts"] as? NSArray {
for element in forecast {
if let dict = element as? [String: AnyObject],
let dates = dict ["validFrom"] as? String ,
let weatherCode = dict ["weatherCode"] as? Int ,
let weatherCodeString = weatherCodesTab[weatherCode],
let temp = dict ["airTemperatureInCelsius"] as? Double ,
let cloud = dict ["effectiveCloudCoverInPercent"] as? Double ,
let rtemp = dict ["feelsLikeTemperatureInCelsius"] as? Double ,
let cloudBase = dict ["cloudBaseHeightInMeter"] as? Double ,
let dewp = dict ["dewPointTemperatureInCelsius"] as? Double ,
let press = dict ["airPressureAtSeaLevelInHectoPascal"] as? Double ,
let hydro = dict ["relativeHumidityInPercent"] as? Double ,
let vent = dict ["windSpeedInKilometerPerHour"] as? Double ,
let rain = dict ["precipitationProbabilityInPercent"] as? Double ,
let iso0 = dict ["freezingLevelHeightInMeter"] as? Double ,
let freezeRain = dict ["freezingRainProbabilityInPercent"] as? Double ,
let noSnow = dict ["noSnowPossibleBelowHeightInMeter"] as? Double ,
let snowUp = dict ["snowCertainAboveHeightInMeter"] as? Double ,
let visibility = dict ["visibilityInMeter"] as? Double ,
let snowProd = dict ["snowfallProbabilityInPercent"] as? Double {
self.datas.append(WeatherProData(date: (DateHelper.obtenir.jourDeLaSemaineWeather(dates)!) + " " + DateHelper.obtenir.dateWeatherPro(dates)! + " à " + (DateHelper.obtenir.heures(dates)!), weatherCode: weatherCodeString, temp: temp, cloud: cloud, rtemp: rtemp, cloudBase: cloudBase, dewp: dewp, press: press, hydro: hydro, vent: vent, rain: rain, iso0: iso0, freezeRain: freezeRain, noSnow: noSnow, snowUp: snowUp, visibility: visibility, snowProb: snowProd))
//print(weatherCodeString)
self.tableView.reloadData()
}
}
}
}
}
}
}
The model:
import UIKit
class WeatherProData {
private var _date: String
private var _weatherCode: String
private var _temp: Double
private var _cloud: Double
private var _rTemp: Double
private var _cloudBase: Double
private var _dewp: Double
private var _press: Double
private var _hydro: Double
private var _vent: Double
private var _rain: Double
private var _iso0: Double
private var _freezeRain: Double
private var _noSnow: Double
private var _snowUp: Double
private var _visibility: Double
private var _snowProb: Double
var date: String {
return _date
}
var weatherCode: String {
return _weatherCode
}
var temp: Double {
return _temp
}
var cloud: Double {
return _cloud
}
var rtemp: Double {
return _rTemp
}
var cloudBase: Double {
return _cloudBase
}
var dewp: Double {
return _dewp
}
var press: Double {
return _press
}
var hydro: Double {
return _hydro
}
var vent: Double {
return _vent
}
var rain: Double {
return _rain
}
var iso0: Double {
return _iso0
}
var freezeRain: Double {
return _freezeRain
}
var noSnow: Double {
return _noSnow
}
var snowUp: Double {
return _snowUp
}
var visibility: Double {
return _visibility
}
var snowProb: Double {
return _snowProb
}
init(date: String, weatherCode: String, temp: Double, cloud: Double, rtemp: Double, cloudBase: Double, dewp: Double, press: Double, hydro: Double, vent: Double, rain: Double, iso0: Double, freezeRain: Double, noSnow: Double, snowUp: Double, visibility: Double, snowProb: Double) {
_date = date
_weatherCode = weatherCode
_temp = temp
_cloud = cloud
_rTemp = rtemp
_cloudBase = cloudBase
_dewp = dewp
_press = press
_hydro = hydro
_vent = vent
_rain = rain
_iso0 = iso0
_freezeRain = freezeRain
_noSnow = noSnow
_snowUp = snowUp
_visibility = visibility
_snowProb = snowProb
}
}
The cell:
import UIKit
class WeatherProCell: UITableViewCell {
#IBOutlet weak var date: UILabel!
#IBOutlet weak var temp: UILabel!
#IBOutlet weak var weatherCode: UILabel!
#IBOutlet weak var tempIcon: UIImageView!
#IBOutlet weak var textIsHidden: UILabel! {
didSet {
textIsHidden.isHidden = true
}
}
#IBOutlet weak var textIsHidden1: UILabel! {
didSet {
textIsHidden1.isHidden = true
}
}
#IBOutlet weak var textIsHidden2: UILabel! {
didSet {
textIsHidden2.isHidden = true
}
}
#IBOutlet weak var textIsHidden3: UILabel! {
didSet {
textIsHidden3.isHidden = true
}
}
#IBOutlet weak var textIsHidden4: UILabel! {
didSet {
textIsHidden4.isHidden = true
}
}
#IBOutlet weak var textIsHidden5: UILabel! {
didSet {
textIsHidden5.isHidden = true
}
}
#IBOutlet weak var textIsHidden6: UILabel! {
didSet {
textIsHidden6.isHidden = true
}
}
#IBOutlet weak var textIsHidden7: UILabel! {
didSet {
textIsHidden7.isHidden = true
}
}
#IBOutlet weak var textIsHidden8: UILabel! {
didSet {
textIsHidden8.isHidden = true
}
}
#IBOutlet weak var textIsHidden9: UILabel! {
didSet {
textIsHidden9.isHidden = true
}
}
#IBOutlet weak var cloud: UILabel! {
didSet {
cloud.isHidden = true
}
}
#IBOutlet weak var rTemp: UILabel! {
didSet {
rTemp.isHidden = true
}
}
#IBOutlet weak var cloudBase: UILabel! {
didSet {
cloudBase.isHidden = true
}
}
#IBOutlet weak var dewp: UILabel! {
didSet {
dewp.isHidden = true
}
}
#IBOutlet weak var press: UILabel! {
didSet {
press.isHidden = true
}
}
#IBOutlet weak var pressIcon: UIImageView! {
didSet {
pressIcon.isHidden = true
}
}
#IBOutlet weak var hydro: UILabel! {
didSet {
hydro.isHidden = true
}
}
#IBOutlet weak var hydroIcon: UIImageView! {
didSet {
hydroIcon.isHidden = true
}
}
#IBOutlet weak var vent: UILabel! {
didSet {
vent.isHidden = true
}
}
#IBOutlet weak var ventIcon: UIImageView! {
didSet {
ventIcon.isHidden = true
}
}
#IBOutlet weak var rainIcon: UIImageView! {
didSet {
rainIcon.isHidden = true
}
}
#IBOutlet weak var rain: UILabel! {
didSet {
rain.isHidden = true
}
}
#IBOutlet weak var iso0: UILabel! {
didSet {
iso0.isHidden = true
}
}
#IBOutlet weak var freezeRain: UILabel! {
didSet {
freezeRain.isHidden = true
}
}
#IBOutlet weak var noSnow: UILabel! {
didSet {
noSnow.isHidden = true
}
}
#IBOutlet weak var snowUp: UILabel! {
didSet {
snowUp.isHidden = true
}
}
#IBOutlet weak var visibility: UILabel! {
didSet {
visibility.isHidden = true
}
}
#IBOutlet weak var snowProb: UILabel! {
didSet {
snowProb.isHidden = true
}
}
var data: WeatherProData!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
func creerCell(_ data: WeatherProData) {
self.data = data
let attributedDate = NSMutableAttributedString(string: self.data.date)
date.attributedText = attributedDate
let Cloud = "\(self.data.cloud)" + " %"
cloud.text = Cloud
let Rtemp = "\(self.data.rtemp)" + " °C"
rTemp.text = Rtemp
let CloudBase = "\(self.data.cloudBase)" + " m"
cloudBase.text = CloudBase
let Dewp = "\(self.data.dewp)" + " °C"
dewp.text = Dewp
let Temp = "\(self.data.temp)" + " °C"
temp.text = Temp
tempIcon.image = #imageLiteral(resourceName: "Termometre")
let WeatherCode = "\(self.data.weatherCode)"
weatherCode.text = WeatherCode
let Press = "\(self.data.press)" + " hpa"
press.text = Press
pressIcon.image = #imageLiteral(resourceName: "Barometre")
let Hydro = "\(self.data.hydro)" + " %"
hydro.text = Hydro
hydroIcon.image = #imageLiteral(resourceName: "Hydrometrie")
let Vent = "\(self.data.vent)" + " km/h"
vent.text = Vent
ventIcon.image = #imageLiteral(resourceName: "Vent")
let Rain = "\(self.data.rain)" + " %"
rain.text = Rain
rainIcon.image = #imageLiteral(resourceName: "Pluviometre")
let Iso0 = "\(self.data.iso0)" + " m"
iso0.text = Iso0
let FreezeRain = "\(self.data.freezeRain)" + " %"
freezeRain.text = FreezeRain
let NoSnow = "\(self.data.noSnow)" + " m"
noSnow.text = NoSnow
let SnowUp = "\(self.data.snowUp)" + " m"
snowUp.text = SnowUp
let Visibility = "\(self.data.visibility)" + " m"
visibility.text = Visibility
let SnowProb = "\(self.data.snowProb)" + " %"
snowProb.text = SnowProb
}
}
I thank you in advance for your help because I have been fighting with this for 1 month
Here is the link to my GitHub to get the complete file:
https://github.com/superdevil669/MeteoWapi_iOS_App/tree/beta
One way would be to modify your data model having an array holding the dates (the values for the sections) and a dictionary holding an array of the weather values for each date.
For the sake of simplicity I have 'simulated' this with String as data type.
// Declaration Data model
var sections = [Date]()
var datas = [Date:[String]]()
// Some test values to show how the processing could be
let date = Date()
let value = "Test"
// This would be inside your processing loop
// Check, if section already exists
if !sections.contains(date)
{
// Add date to sections array
sections.append(date)
// Insert empty array in datas dictionary
datas[date] = [String]()
}
// Add value to array
datas[date]?.append(value)
// This could be inside cellForRowAtIndexPath
// Fetch value
let indexPath = IndexPath(row: 0, section: 0)
let value2 = datas[sections[indexPath.section]]?[indexPath.row]
First of all private backing variables to get constants is horrible in Swift. You can declare constants simply with the let keyword.
If you use a struct you can even reduce WeatherProData to
struct WeatherProData {
let date: String // must be `Date` for smart grouping
let weatherCode: String
let temp: Double
let cloud: Double
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
}
because you get the init method for free
Grouping an array is very easy in Swift. The suggestion below assumes that date is declared and decoded as Date.
Dictionary(grouping:by: groups the array by the condition in the closure in this case midnight of the current date.
let calendar = Calendar.current
let grouped = Dictionary(grouping: datas, by: { calendar.startOfDay(for: $0.date) })
print(grouped)
And in Swift 4.2+ you can negate a boolean with
cell.textIsHidden.isHidden.toggle()
Consider also to use the Deodable protocol to get rid of the manual decoding.
I tried another project so as not to damage mine.
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!
var data = [WeatherProData]()
var sections = [Date]()
var dataT = [Date:[String]]()
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
let date = Date()
let value = "Test"
if !sections.contains(date) {
sections.append(date)
dataT[date] = [String]()
}
dataT[date]?.append(value)
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)
// 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: AnyObject] {
if let forecast = JSON ["forecasts"] as? NSArray {
for element in forecast {
if let dict = element as? [String: AnyObject],
let dates = dict ["validFrom"] as? String {
self.data.append(WeatherProData(date: dates))
print(dates)
self.tableView.reloadData()
}
}
}
}
}
}
func numberOfSections(in tableView: UITableView) -> Int {
return dataT.count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "test"
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let datas = data[indexPath.row]
cell.textLabel?.text = datas.date
return cell
}
override func viewWillAppear(_ animated: Bool) {
self.tableView.reloadData()
}
}
I did the simplest thing, but I can't classify my UITableView not section I think it must be due to the JSON file that sends me this as date.
2019-01-14T10:34:04Z
2019-01-14T12:00:00+01:00
2019-01-14T13:00:00+01:00
2019-01-14T14:00:00+01:00
2019-01-14T15:00:00+01:00
2019-01-14T16:00:00+01:00
2019-01-14T17:00:00+01:00
2019-01-14T18:00:00+01:00
2019-01-14T19:00:00+01:00
2019-01-14T20:00:00+01:00
2019-01-14T21:00:00+01:00
2019-01-14T22:00:00+01:00
2019-01-14T23:00:00+01:00
2019-01-15T00:00:00+01:00
2019-01-15T01:00:00+01:00
2019-01-15T02:00:00+01:00
2019-01-15T03:00:00+01:00
2019-01-15T04:00:00+01:00
2019-01-15T05:00:00+01:00
2019-01-15T06:00:00+01:00
2019-01-15T07:00:00+01:00
2019-01-15T08:00:00+01:00
2019-01-15T09:00:00+01:00
2019-01-15T10:00:00+01:00
2019-01-15T11:00:00+01:00
Thank you. Thank you.
I'm still trying. I can create my sections but I can't display them without the hours.
I would like each section to have the title of the day, and below that the hours of each day.
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
}
}
This is what I get.
My UITableView
In
self.sections = Array(Set(self.data.map({ (WeatherProData) -> String in
WeatherProData.date
})))
, when you have -> String means that the function is asking you to return a string value. So you need to add a return with the string value in your code.
Here is a working code for your situation :
self.sections = Array(Set(self.data.map({ (WeatherProData) -> String in
let jsonDateString = WeatherProData.date
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssXXX"
if let weatherProDataDate = dateFormatter.date(from: jsonDateString) {
return french.string(from: weatherProDataDate)
} else {
return ""
}
})))
i have a problem,when i called the viewWillAppear ,table view get refreshed,but the data are strange(i insert a new record so i need to refresh the table view every time it appears), for example
original data:a,b,c
insert one record should be:a,b,c,d
result:a,b,c,a(why a?)
Hope someone could help me ,please.
import UIKit
import SQLite
class DetailViewController: UIViewController, UITableViewDelegate,
UITableViewDataSource {
#IBOutlet weak var tableView: UITableView!
let inputDetailTable = Table("input_detail")
var labelOneArray = [String]()
var labelTwoArray = [NSAttributedString]()
var labelThreeArray = [String]()
var labelFourArray = [String]()
let id = Expression<Int>("id")
let deleteFlag = Expression<Int>("delete_flag")
// var selectedDatas :AnySequence<Row>!
let viewService = ViewService()
override func viewDidLoad() {
super.viewDidLoad()
getTableViewData()
}
override func viewWillAppear(_ animated: Bool) {
getTableViewData()
self.tableView.reloadData()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection
section: Int) -> Int {
let selectedDatas = viewService.selectTableData()
var counter = 0
for _ in selectedDatas{
counter += 1
}
return counter
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath:
IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell",
for: indexPath) as! DetailTableViewCell
cell.labelOne.attributedText = labelTwoArray[indexPath.row]
cell.labelTwo.text = labelFourArray[indexPath.row]
return cell
}
func getTableViewData() {
let selectedDatas = viewService.selectTableData()
for selectedData in selectedDatas{
var tempAmount = String(selectedData[Expression<Int>
("amount")])
var tempTwoAmout :NSAttributedString?
if selectedData[Expression<Int>("type_flag")] == 0 {
tempAmount = "-"+tempAmount
let text = tempAmount
let nsText = text as NSString
let textRange = NSMakeRange(0, nsText.length)
let myMutableString = NSMutableAttributedString(
string: tempAmount,
attributes: [:])
myMutableString.addAttribute(
NSAttributedStringKey.foregroundColor,
value: UIColor.red,
range: textRange)
tempTwoAmout = myMutableString
} else {
tempAmount = "+"+tempAmount
let text = tempAmount
let nsText = text as NSString
let textRange = NSMakeRange(0, nsText.length)
let myMutableString = NSMutableAttributedString(
string: tempAmount,
attributes: [:])
myMutableString.addAttribute(
NSAttributedStringKey.foregroundColor,
value: UIColor.green,
range: textRange)
tempTwoAmout = myMutableString
}
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd hh:mm:ss"
let createTimeFormat = formatter.string(from:
selectedData[Expression<Date>("create_time")])
self.labelOneArray.append(String(selectedData[Expression<Int>
("id")]))
self.labelTwoArray.append(tempTwoAmout!)
self.labelThreeArray.append(String(selectedData[Expression<String>
("location")]))
self.labelFourArray.append(createTimeFormat)
}
}
}
You are calling twice getTableViewData(), and try to remove all data from your arrays before getting new data.
I am getting error in ToDoViewController.Swift here is the overall code:
I am makeing a simple app for taking orders from customers. and orders will store in Firebase.
View Controller.Swift
import UIKit
import Firebase
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var tableView: UITableView!
var todoList = [Todo]()
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
loadData()
}
func loadData() {
self.todoList.removeAll()
let ref = FIRDatabase.database().reference()
ref.child("todoList").observeSingleEvent(of: .value, with: { (snapshot) in
if let todoDict = snapshot.value as? [String:AnyObject] {
for (_,todoElement) in todoDict {
print(todoElement);
let todo = Todo()
todo.name = todoElement["name"] as? String
todo.message = todoElement["message"] as? String
todo.reminderDate = todoElement["date"] as? String
self.todoList.append(todo)
}
}
self.tableView.reloadData()
}) { (error) in
print(error.localizedDescription)
}
}
//MARK: TableView datasource
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.todoList.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ToDoCell")
cell!.textLabel?.text = todoList[indexPath.row].name
return cell!
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
ToDoViewController
import UIKit
import Firebase
class ToDoViewController: UIViewController {
var todo:Todo?
#IBOutlet weak var nameField: UITextField!
#IBOutlet weak var messageField: UITextField!
#IBOutlet weak var dateFormatter: UIDatePicker!
#IBAction func Done(_ sender: Any) {
if todo == nil {
todo = Todo()
}
// first section
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd/MM/yyyy hh:mm"
todo?.name = self.nameField.text
todo?.message = self.messageField.text
todo?.reminderDate = dateFormatter.string(from: self.dateFormatter.date)
//second section
let ref = FIRDatabase.database().reference()
let key = ref.child("todoList").childByAutoId().key
let dictionaryTodo = [ "name" : todo!.name! ,
"message" : todo!.message!,
"date" : todo!.reminderDate!]
let childUpdates = ["/todoList/\(key)": dictionaryTodo]
ref.updateChildValues(childUpdates, withCompletionBlock: { (error, ref) -> Void in
self.navigationController?.popViewController(animated: true)
})
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let todoVC = self.storyboard!.instantiateViewController(withIdentifier: "ToDoVC") as! ToDoViewController
todoVC.todo = todoList[indexPath.row]
self.navigationController?.pushViewController(todoVC, animated: true)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
if self.todo != nil {
nameField.text = self.todo?.name
messageField.text = self.todo?.message
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd/MM/yyyy hh:mm"
let date = dateFormatter.date(from: self.todo!.reminderDate!)
datePicker.date = date!
}
}
}
Todo.Swift
import UIKit
class Todo: NSObject {
var name :String?
var message: String?
var reminderDate: String?
// id which is set from firebase to uniquely identify it
var uniqueId:String?
}
Something is wrong with your code:
Use of un-resolved identifier 'todoList':
todoList is declared in View Controller.Swift and you cannot use it in ToDoViewController.
Cannot assign to property: 'date' is a method:
In the code datePicker is undefined and #IBOutlet weak var dateFormatter: UIDatePicker! should be:
#IBOutlet weak var datePicker: UIDatePicker!
AND In the image dateFormatter.date = date! should be:
datePicker.date = date!
enter code hereI'm trying to add a calendar feature to my app, but I'm having 2 errors on two different viewcontrollers, I can't find out what I am missing.
The error says:
Value of type AddEventViewController has no member "Calendar" on the line addEventVC.calendar = calendar
2nd viewcontroller
1st viewcontroller
Code:
import UIKit
import EventKit
class EventsViewController: UIViewController, UITableViewDataSource, EventAddedDelegate {
#IBOutlet weak var tableView: UITableView!
var calendar: EKCalendar!
var events: [EKEvent]?
#IBOutlet weak var eventsTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
loadEvents()
}
func loadEvents() {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let startDate = dateFormatter.date(from: "2016-01-01")
let endDate = dateFormatter.date(from: "2016-12-31")
if let startDate = startDate, let endDate = endDate {
let eventStore = EKEventStore()
let eventsPredicate = eventStore.predicateForEvents(withStart: startDate, end: endDate, calendars: [calendar])
self.events = eventStore.events(matching: eventsPredicate).sorted {
(e1: EKEvent, e2: EKEvent) in
return e1.startDate.compare(e2.startDate) == ComparisonResult.orderedAscending
}
}
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let events = events {
return events.count
}
return 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "basicCell")!
cell.textLabel?.text = events?[(indexPath as NSIndexPath).row].title
cell.detailTextLabel?.text = formatDate(events?[(indexPath as NSIndexPath).row].startDate)
return cell
}
func formatDate(_ date: Date?) -> String {
if let date = date {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM/dd/yyyy"
return dateFormatter.string(from: date)
}
return ""
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destinationVC = segue.destination as! UINavigationController
let addEventVC = destinationVC.childViewControllers[0] as! AddEventViewController
addEventVC.calendar = calendar
addEventVC.delegate = self
}
// MARK: Event Added Delegate
func eventDidAdd() {
self.loadEvents()
self.eventsTableView.reloadData()
}
}
AddEventViewController :
class AddEventViewController: UIViewController {
#IBOutlet weak var eventNameTextField: UITextField!
#IBOutlet weak var eventStartDatePicker: UIDatePicker!
#IBOutlet weak var eventEndDatePicker: UIDatePicker!
var delegate: EventAddedDelegate?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.eventStartDatePicker.setDate(initialDatePickerValue(), animated: false)
self.eventEndDatePicker.setDate(initialDatePickerValue(), animated: false)
}
func initialDatePickerValue() -> Date {
let calendarUnitFlags: NSCalendar.Unit = [.year, .month, .day, .hour, .minute, .second]
var dateComponents = (Calendar.current as NSCalendar).components(calendarUnitFlags, from: Date())
dateComponents.hour = 0
dateComponents.minute = 0
dateComponents.second = 0
return Calendar.current.date(from: dateComponents)!
}
#IBAction func cancelButtonTapped(_ sender: UIButton) {
self.dismiss(animated: true, completion: nil)
}
#IBAction func addEventButtonTapped(_ sender: UIButton) {
// Create an Event Store instance
let eventStore = EKEventStore();
// Use Event Store to create a new calendar instance
if let calendarForEvent = eventStore.calendar(withIdentifier: self.calendar.calendarIdentifier)
{
let newEvent = EKEvent(eventStore: eventStore)
newEvent.calendar = calendarForEvent
newEvent.title = self.eventNameTextField.text ?? "Some Event Name"
newEvent.startDate = self.eventStartDatePicker.date
newEvent.endDate = self.eventEndDatePicker.date
// Save the event using the Event Store instance
do {
try eventStore.save(newEvent, span: .thisEvent, commit: true)
delegate?.eventDidAdd()
self.dismiss(animated: true, completion: nil)
} catch {
let alert = UIAlertController(title: "Event could not save", message: (error as NSError).localizedDescription, preferredStyle: .alert)
let OKAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alert.addAction(OKAction)
self.present(alert, animated: true, completion: nil)
}
}
}
}
You are calling addEventVC.calendar = calendar but your AddEventViewController doesn't have a variable called calendar which is why you get the error AddEventViewController has no member "Calendar".
I have error __NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0] when I try to add second item with the same date.
I can't find where is the issue. I think problem is in getSectionItems function.
Someone can help me?
#IBOutlet weak var tableView: UITableView!
#IBOutlet weak var sumLabel: UILabel!
var costItems: NSMutableArray = NSMutableArray()
var accountIdentifier: NSString = NSString()
var costsSum:Int!
var sectionsInTable = [String]()
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.delegate = self
self.tableView.dataSource = self
costsSum = getCostsSum()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewDidAppear(animated: Bool) {
loadData()
self.tableView.reloadData()
costsSum = getCostsSum()
}
func loadData() {
costItems.removeAllObjects()
let moc:NSManagedObjectContext = SwiftCoreDataHelper.managedObjectContext()
let predicate: NSPredicate = NSPredicate(format: "identifier == '\(accountIdentifier)'")!
let results:NSArray = SwiftCoreDataHelper.fetchEntities(NSStringFromClass(Cost), withPredicate: predicate, managedObjectContext: moc)
for cost in results{
let singleCost:Cost = cost as Cost
let costDict:NSDictionary = ["identifier":singleCost.identifier,"costName":singleCost.costName, "costValue":singleCost.costValue, "date":singleCost.date]
costItems.addObject(costDict)
// Date
let dateFormatter: NSDateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd MMMM"
let dateString: NSString = dateFormatter.stringFromDate(costDict.objectForKey("date") as NSDate)
let sections: NSSet = NSSet(array: sectionsInTable)
if !sections.containsObject(dateString) {
sectionsInTable.append(dateString)
}
}
self.tableView.reloadData()
}
func getSectionItems(section: Int) -> [Cost] {
var sectionItems = [Cost]()
let moc:NSManagedObjectContext = SwiftCoreDataHelper.managedObjectContext()
let predicate: NSPredicate = NSPredicate(format: "identifier == '\(accountIdentifier)'")!
let results:NSArray = SwiftCoreDataHelper.fetchEntities(NSStringFromClass(Cost), withPredicate: predicate, managedObjectContext: moc)
for cost in results {
let singleCost:Cost = cost as Cost
let costDict:NSDictionary = ["identifier":singleCost.identifier,"costName":singleCost.costName, "costValue":singleCost.costValue, "date":singleCost.date]
let dateFormatter: NSDateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd MMMM"
let dateString: NSString = dateFormatter.stringFromDate(costDict.objectForKey("date") as NSDate)
if dateString == sectionsInTable[section] as NSString {
sectionItems.append(singleCost)
}
}
return sectionItems
}
// UITableViewDataSource
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sectionsInTable[section]
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return sectionsInTable.count
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.getSectionItems(section).count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:DetailTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as DetailTableViewCell
let costDict:NSDictionary = costItems.objectAtIndex(indexPath.row) as NSDictionary
let costName = costDict.objectForKey("costName") as String
let costValue = costDict.objectForKey("costValue") as Double
cell.nazwaWydatkuLabel.text = costName
// Date
let dateFormatter: NSDateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd MMMM"
let dateString: NSString = dateFormatter.stringFromDate(costDict.objectForKey("date") as NSDate)
cell.dateLabel.text = dateString
if (Int(costValue)) < 0 {
cell.wartośćLabel.layer.cornerRadius = 8.0
cell.wartośćLabel.layer.masksToBounds = true
cell.wartośćLabel.backgroundColor = UIColor(red: 255.0/255.0, green: 59.0/255, blue: 48.0/255.0, alpha: 1.0)
} else {
cell.wartośćLabel.layer.cornerRadius = 8.0
cell.wartośćLabel.layer.masksToBounds = true
cell.wartośćLabel.backgroundColor = UIColor(red: 127.0/255.0, green: 207.0/255.0, blue: 77.0/255.0, alpha: 1.0)
}
cell.wartośćLabel.text = " \(Int(costValue)) zł "
return cell
}
#IBAction func addCostButtonPressed(sender: UIBarButtonItem) {
self.performSegueWithIdentifier("addCostVC", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "addCostVC" {
let addCostVC: AddTableViewController = segue.destinationViewController as AddTableViewController
let thisID = accountIdentifier
addCostVC.accountIdentifier = thisID
}
}
func getCostsSum() -> Int {
let moc:NSManagedObjectContext = SwiftCoreDataHelper.managedObjectContext()
let predicate: NSPredicate = NSPredicate(format: "identifier == '\(accountIdentifier)'")!
let results:NSArray = SwiftCoreDataHelper.fetchEntities(NSStringFromClass(Cost), withPredicate: predicate, managedObjectContext: moc)
var costSum: Int = 0
for res in results {
var costCount = res.valueForKey("costValue") as Int
costSum += costCount
}
self.sumLabel.text = "\(costSum) zł"
return costSum
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
let moc:NSManagedObjectContext = SwiftCoreDataHelper.managedObjectContext()
let results:NSArray = SwiftCoreDataHelper.fetchEntities(NSStringFromClass(Cost), withPredicate: nil, managedObjectContext: moc)
let thisCost: AnyObject = results.objectAtIndex(indexPath.row)
moc.deleteObject(thisCost as NSManagedObject)
//costsValues = getAccountCountSum()
SwiftCoreDataHelper.saveManagedObjectContext(moc)
loadData()
getCostsSum()
self.tableView.reloadData()
}