title: How to add section to UITableView programmatically in swift 3
body: <p>I need to list contents of a json and sort by date (sections). So far, I managed to get the json successfully and list the main content (name and hour) but I need to sort by date as section.</p>
  
  <p><strong>EDIT:</strong>
  If I have more than 100 "items" (json objects) and in some the date is repeated (as in the first two objects of the json below), then I want those items to be in a section with the date as the title of the each section.</p>
  
  <p><strong>Json:</strong></p>
  
  <pre><code>[
  {"date_raw":"1/18/2017 12:00:00 AM","name":"Hello 2","hour":"12:00 - 14:00","audio_url":"http://example.com/file2.mp4"},
  {"date_raw":"1/18/2017 12:00:00 AM","name":"Hello 1","hour":"10:00 - 12:00","audio_url":"http://example.com/file1.mp4"},
  {"date_raw":"1/17/2017 12:00:00 AM","name":"Hello","hour":"10:00 - 12:00","audio_url":"http://example.com/file.mp4"},
  {"date_raw":"1/16/2017 12:00:00 AM","name":"Hello","hour":"10:00 - 12:00","audio_url":"http://example.com/file.mp4"}
  ]
  </code></pre>
  
  <p><strong>Main Class</strong>:</p>
  
  <pre><code>class MyVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
  
      @IBOutlet weak var tableView: UITableView!
  
      // date_raw
      var section: [String] = []
  
      // date_raw, name, hour, audio_url
      var items: [(String, String, String, String)] = []
  
      var dateV:String = ""
  
      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
          return items.count
  
      }
  
  
      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
  
          let cell:UITableViewCell = UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: "cell")
  
          cell.textLabel!.text = items[indexPath.row].1 // get name
          cell.detailTextLabel!.text = items[indexPath.row].2 // get hour
  
          return cell;
      }
  
  
      func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
          print(items[indexPath.row])
  
      }
  
  
      func getJson(_ url:String) {
  
          // Asynchronous Http call to your api url, using NSURLSession:
          URLSession.shared.dataTask(with: URL(string: url)!, completionHandler: { (data, response, error) -&gt; Void in
  
              if error != nil || data == nil {
  
                  print("error")
  
              } else {
  
  
                  do {
  
                      let json:NSArray = try (JSONSerialization.jsonObject(with: data!, options:JSONSerialization.ReadingOptions.mutableContainers) as? NSArray)!
  
                      for i in 0...json.count-1 {
  
                          if let date_raw = (json[i] as AnyObject).value(forKey: "date_raw") as? String,
                              let name = (json[i] as AnyObject).value(forKey: "name") as? String,
                              let hour = (json[i] as AnyObject).value(forKey: "hour") as? String,
                              let audio_url = (json[i] as AnyObject).value(forKey: "audio_url") as? String {
  
                              if date_raw != self.dateV {
                                  self.section.append(date_raw)
                                  self.dateV = date_raw;
                              }
  
                              self.items.append(date_raw, name, hour, audio_url)
  
                          }
                      }//End for
  
                      //print(self.section)
                      self.do_table_refresh();
  
                  } catch {
                      print("error")
                  }
  
              }
  
          }).resume()
          // TODO: butonu disable et
      }
  
  
      func do_table_refresh() {
          DispatchQueue.main.async(execute: {
              self.tableView.reloadData()
              self.tableView.isHidden = false
              return
          })
      }
  
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          tableView.delegate      =   self
          tableView.dataSource    =   self
          tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
          tableView.isHidden = true
  
          getJson("http://example.com/my-json-file.json")
  
      }
  
  
  }
  </code></pre>