title: UITableView with Multiple Sections using Realm and Swift
body: <p>Ok, so I found a lot of information regarding UITableView and multiple sections, however, they are always with strings, arrays, static data, Obj-C or something else with which I am unable to translate to my situation, mainly because I am completely new to developing apps. Any help is greatly appreciated since it has been a little over a month that I have been trying different approaches without success.</p>
  
  <p>So I have multiple Dog objects with the following properties:</p>
  
  <pre><code>class Dog: Object {
      dynamic var name = ""
      dynamic var race = ""
      dynamic var age = 0
      dynamic var owner = ""
      dynamic var dogID = ""
  
      override static func primaryKey() -&gt; String? {
          return "dogID"
      }
  }
  </code></pre>
  
  <p>And in my ViewController file I have the following code (I removed the irrelevant lines):</p>
  
  <pre><code>let realm = try! Realm()
  var dogResults : Results&lt;Dog&gt;?
  
  override func viewDidLoad() {
      super.viewDidLoad()
  
      self.dogResults = realm.objects(Dog.self)
  
  }
  
  func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
      return self.dogResults!.count
  }
  
  func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell {
      let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
      let dog = self.dogResults![indexPath.row]
      cell.textLabel?.text = dog.name
  }
  
  func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -&gt; String? {
          // ?
  }
  
  func numberOfSectionsInTableView(tableView: UITableView) -&gt; Int {
          // ?
  }
  </code></pre>
  
  <p>I would like to organize the sections by the "race" property from the Dog object, however I am having a very difficult time achieving this.</p>
  
  <p>I saw a few examples where they use the "if" statement for the sections, I have tried that and was not able to get the proper results, however, I would like the cleaner approach I have seen in some other examples, using:</p>
  
  <pre><code>override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
      return data[section].count
  }
  </code></pre>
  
  <p>I have made various attempts, but being a Realm DB and swift I am having problems following most of the examples.</p>
  
  <p>Thank you for your time.</p>