title: How to use external datasource in UITableView
body: <p>I have a <code>UIViewController</code> in which I've embedded a <code>UITableView</code>. If added an outlet to my ViewController class.</p>
  
  <p>Because I don't want the <code>ViewController</code> to get too heavy, I would like to put the methods of the <code>UITableViewDataSource</code> protocol and <code>UITableViewDelegate</code> protocol into separate classes.</p>
  
  <p>So I created a TableViewDataSource.swift and a TableViewDelegate class:</p>
  
  <pre><code>class TableViewDelegate : NSObject, UITableViewDelegate {
      //custom code here if required
  }
  
  class TableViewDataSource : NSObject, UITableViewDataSource {
      func numberOfSectionsInTableView(tableView: UITableView) -&gt; Int {
          return 1
      }
      func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
  
         return 1
      }
       func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell {
  
          let cellIdentifier = "MyCellIdentifier"
          let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath)
  
          cell.textLabel?.text = "left text"
          cell.detailTextLabel?.text = "right text"
  
          return cell
  
  
      }
  
  
  }
  
  class ViewController: UIViewController {
  
      @IBOutlet var tableView: UITableView!
      override func viewDidLoad() {
          super.viewDidLoad()
  
          tableView.dataSource = TableViewDataSource()
          tableView.delegate = TableViewDelegate()
  
          tableView.setNeedsDisplay()
  
          // Do any additional setup after loading the view, typically from a nib.
      }
  }
  </code></pre>
  
  <p>In my Storyboard, I've created a prototype cell within the tableView with the identifier "MyCellIdentifier". I use this identifier to create a cell in my TableViewDataSource method.</p>
  
  <p>However, if I run the app, I only see an empty tableView. The methods don't even get called even though I have set them in the viewDidLoad method of the ViewController. I also tried to enforce the tableView to redraw by calling setNeedsDisplay. This had no effect, too.</p>
  
  <p><a href="https://i.stack.imgur.com/Ooiq4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Ooiq4.png" alt="enter image description here"></a> </p>