4

I have a Table view with data compiled from a Dictionary array where the keys are the section headers:

var data: Dictionary<String,[String]> = [
    "Breakfast": ["Oatmeal","Orange Juice"],
    "lunch": ["Steak","Mashed Potatoes","Mixed Veg"],
    "Dinner": ["Chicken","Rice"],
    "Snack": ["Nuts","Apple"]

]
var breakfastCalories = [100,200,300]
var lunchCalories = [300,400,500]
var DinnerCalories = [600,700,800]
var breakfast = 0

Below is the code for populating the Table View

override func viewDidLoad() {
    super.viewDidLoad()

    for value in breakfastCalories as NSArray as! [Int]{

    breakfast = breakfast + value

    }

}


func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    return data.count
}

 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // Return the number of rows in the section.
    let sectionString = Array(data.keys)[section]

    return data[sectionString]!.count
}

 func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    let sectionString = Array(data.keys)[section]
    return sectionString
}


 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! TableViewCell
    let sectionString = Array(data.keys)[indexPath.section]


    cell.caloriesLabel.tag = indexPath.row
    cell.caloriesLabel.text = String(breakfastCalories[indexPath.row])
    cell.foodLabel.tag = indexPath.row
    cell.foodLabel.text = data[sectionString]![indexPath.row]

    return cell
}

func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {

let footerView = UIView(frame: CGRectMake(0, 0, tableView.frame.size.width, 40))
//    self.myTableView.tableFooterView = footerView;


    let label = UILabel(frame: CGRectMake(footerView.frame.origin.x - 15, footerView.frame.origin.y, footerView.frame.size.width, 20))
    label.textAlignment = NSTextAlignment.Right

    label.text = "Total Calories: \(breakfast) "

    footerView.addSubview(label)

    return footerView
}

 func tableView(tableView: UITableView, heightForFooterInSection section: Int) ->  CGFloat {

    return 20.0
}

My question is, how can I add the calories array for each section? so for breakfast, it will contain the calories from the breakfastCalories array, lunch section for the lunchCalories array etc.

I may be overthinking this but I can't get my head around this problem

Thanks

This is my table view. On the right the values are grabbed from the breakfastCalories however as mentioned each section contain the calories from the breakfastCalories array, lunch section for the lunchCalories array etc.

1 Answer 1

4

You could structure your calories similar to your data property, with similar keys:

var calories: Dictionary<String,[Int]> = [
"Breakfast": [100,200,300],
"lunch": [300,400,500],
"Dinner": [600,700,800]
]

That way you can pull out the right calories depending on what section you are showing and add them up to create a total for you label to show: (Add this where you create your footerView and just above where you set your label.text)

let dataKeysArray = Array(data.keys)[section]
let sectionString = dataKeysArray[section]
let mealCalories = calories[sectionString]

var totalCalories: Int = 0
for calories in mealCalories {
    totalCalories += calories
}

label.text = "Total Calories: \(totalCalories) "
Sign up to request clarification or add additional context in comments.

3 Comments

this solution does make sense, however I am getting a the unexpectedly found nil error on mealCalories
Nevermind, just a silly mistake. I did not add calories for my snacks so it is set to nil. Thank you for your swift solution
@JoeBenton I have a similar question that adds the total of the cells into the footer, do you think you could help me with it stackoverflow.com/questions/59039423/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.