title: How do you add a second section with a different type of prototype cell to a tableview in Swift?
body: <p>I have a tableview with 2 prototype cells in a view controller. I want each cell to display data from different arrays. </p>
  
  <p>Below is my code. I can get the tableview to show jobs or schools, but not both. I don't understand how to make the table display cell1 (jobs) and then cell2 (schools) when each cell contains different data sources.</p>
  
  <p><a href="https://i.stack.imgur.com/keHkF.png" rel="nofollow noreferrer">screenshot</a></p>
  
  <pre><code>Import UIKit
  
  class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
  
  
  let jobs = ["McDonalds", "Hardees", "Taco Bell"]
  let schools = ["Univ of CO", "Univ of TX", "Univ of CA"]
  
  
  override func viewDidLoad() {
      super.viewDidLoad()
  
  }
  
  
  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
  
      return jobs.count
  
  }
  
  
  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
  
          let cell = tableView.dequeueReusableCell(withIdentifier: "JobsCell", for: indexPath) as! Jobs
  
          let job = jobs[indexPath.row]
          cell.jobLbl.text = job
  
          return cell
  
  
      }
  
  }
  </code></pre>
  
  <p>Answer: I fixed this by adding numberOfSections function (thanks Robert). Updated code below if anyone else has this question:</p>
  
  <p>import UIKit</p>
  
  <p>class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {</p>
  
  <pre><code>let jobs = ["McDonalds", "Hardees", "Taco Bell"]
  let schools = ["Univ of CO", "Univ of TX", "Univ of CA", "Univ of Camdenton"]
  
  
  override func viewDidLoad() {
      super.viewDidLoad()
  }
  
  
  func numberOfSections(in tableView: UITableView) -&gt; Int {
      return 2
  }
  
  
  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
  
      if (section == 0) {
          return jobs.count
      } else {
          return schools.count
      }
  
  }
  
  
  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
  
      if indexPath.section == 0 {
  
          let cell = tableView.dequeueReusableCell(withIdentifier: "JobsCell", for: indexPath) as! Jobs
          let job = jobs[indexPath.row]
          cell.jobLbl.text = job
          return cell
  
      } else {
  
          let cell = tableView.dequeueReusableCell(withIdentifier: "SchoolsCell", for: indexPath) as! Schools
          let school = schools[indexPath.row]
          cell.schoolLbl.text = school
          return cell
  
      }
  
  }
  </code></pre>
  
  <p>}</p>