title: Adding specific data for each section in UITableView - Swift
body: <p>I have a Table view with data compiled from a Dictionary array where the keys are the section headers:</p>
  
  <pre><code>var data: Dictionary&lt;String,[String]&gt; = [
      "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
  </code></pre>
  
  <p>Below is the code for populating the Table View</p>
  
  <pre><code>override func viewDidLoad() {
      super.viewDidLoad()
  
      for value in breakfastCalories as NSArray as! [Int]{
  
      breakfast = breakfast + value
  
      }
  
  }
  
  
  func numberOfSectionsInTableView(tableView: UITableView) -&gt; Int {
  
      return data.count
  }
  
   func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -&gt; 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) -&gt; String? {
      let sectionString = Array(data.keys)[section]
      return sectionString
  }
  
  
   func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; 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) -&gt; 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) -&gt;  CGFloat {
  
      return 20.0
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>I may be overthinking this but I can't get my head around this problem</p>
  
  <p>Thanks </p>
  
  <p><a href="https://i.stack.imgur.com/9who9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9who9.png" alt="This is my table view."></a>
   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.</p>