I have a table view with 7 rows Monday, Tuesday,....,Sunday. My app receives a json from a web service which is of format:
({
appointments = (
{
numApts = 1;
scheduleDate = "2015-11-02";
},
{
numApts = 2;
scheduleDate = "2015-11-04";
}
);
})
So I am trying to loop through the json response and update the label of our weekday if it matches a date in the json received.
Not sure how to implement this. Do I need a model class? Something like:
import UIKit
class CurrentRosterModel {
var numApts : String?
var scheduleDate : String?
init(json : NSDictionary){
self.numApts = json["numApts"] as? String
self.scheduleDate = json["scheduleDate"] as? String
}
}
What I was trying today was a function to update the rows text like so, but I wasn't getting into the final if let condition to access the cell in order to update the label:
let weekDateDict = ["Monday" : mon, "Tuesday" : tues, "Wednesday" : wedns, "Thursday" : thurs, "Friday" : fri, "Saturday" : sat, "Sunday" : sun]
//where vars mon = "2015-11-02", tues = "2015-11-03" etc.
//aptsArray is hard coded for now but will need to come from a web service response later
let aptsArray : [Dictionary<String, String>] = [
[
"numApts" : "1",
"scheduleDate" : "2015-11-02"
],
[
"numApts" : "2",
"scheduleDate" : "2015-11-04"
]];
for (weekDay, weekDate) in weekDateDict {
if aptsArray.contains({ $0.values.contains(weekDate)}) {
print("Matched with weekDate is \(weekDate) and weekDay is \(weekDay)")
//getting this condition twice as expected
let ourIndexPath : NSIndexPath?
switch weekDay {
case "Monday":
ourIndexPath = NSIndexPath(forRow: 0, inSection : 0)
//print("Monday label update")
case "Tuesday":
ourIndexPath = NSIndexPath(forRow: 1, inSection : 0)
//print("Tuesday label update")
case "Wednesday":
ourIndexPath = NSIndexPath(forRow: 2, inSection : 0)
//print("Wednesday label update")
case "Thursday":
ourIndexPath = NSIndexPath(forRow: 3, inSection : 0)
//print("Thursday label update")
case "Friday":
ourIndexPath = NSIndexPath(forRow: 4, inSection : 0)
//print("Friday label update")
case "Saturday":
ourIndexPath = NSIndexPath(forRow: 5, inSection : 0)
//print("Saturday label update")
case "Sunday":
ourIndexPath = NSIndexPath(forRow: 6, inSection : 0)
//print("Sunday label update")
default :
ourIndexPath = NSIndexPath(forRow: 7, inSection : 0)
//print("swicth not satisfied")
}
if let cell = weekTableView.cellForRowAtIndexPath(ourIndexPath!) as? WeekDayCell{
print("got in here")//not getting in here
cell.numAptsLbl.text = aptsArray[0]["numApts"]!
weekTableView.beginUpdates()
weekTableView.reloadRowsAtIndexPaths([ourIndexPath!], withRowAnimation: UITableViewRowAnimation.Automatic)
weekTableView.endUpdates()
}
}
My tableview methods look as follows:
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 7
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = weekTableView.dequeueReusableCellWithIdentifier("WeekDayCell", forIndexPath: indexPath) as! WeekDayCell
cell.dayLbl?.text = weekArray[indexPath.row]
cell.numAptsLbl?.text = "0"
//indexPath.row.description
//print("indexpath in tableview is \(indexPath)")
return cell
}