title: UITableView with MVVM using Swift
body: <p>I'm working on <strong>MVVM architecture</strong> in Swift with <code>UITableView</code>. For this, I have created sample table view.</p>
  
  <p>Can any one please suggest whether I am going correct or any other improvements need to do?</p>
  
  <p>The following are the classes for this architecture.</p>
  
  <ol>
  <li><p><strong>ViewController</strong> - Contains <code>UITableView</code>  and its <code>delegate</code> and <code>datasource</code> methods.</p>
  
  <pre><code>class ViewController: UIViewController {
      let PRODUCT_CELL_IDENTIFIER = "ProductCellIdentifier"
  
      @IBOutlet weak var productTableView: UITableView!
  
      var productViewModel: ProductViewModel = ProductViewModel()
  }
  
  //UITableView Delegate Methods
  extension ViewController {
      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
          return productViewModel.numberOfRowsInSection()
      }
  
      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
          let cell = tableView.dequeueReusableCell(withIdentifier: PRODUCT_CELL_IDENTIFIER) as! ProductTableViewCell
          let product = productViewModel.productsArray[indexPath.row]
          cell.productName.text = product.name
          cell.productQuantity.text = "\(product.quantity)"
          return cell
      }
  }
  </code></pre></li>
  <li><p><strong>ProductViewModel:</strong> - This is ViewModel class.</p>
  
  <pre><code>class ProductViewModel: NSObject {
      var productsArray = Array&lt;Product&gt;()
  
      override init() {
          let product1 = Product(name: "Prodcut1", image_url: "", quantity: 2)
          let product2 = Product(name: "Prodcut2", image_url: "", quantity: 3)
          productsArray.append(product1)
          productsArray.append(product2)
      }
  
      func numberOfRowsInSection() -&gt; Int {
          return productsArray.count
      }
  }
  </code></pre></li>
  <li><p><strong>Product</strong> - This is the model class</p>
  
  <pre><code>class Product: NSObject {
      var name: String
      var image_url: String
      var quantity: Int
  
      init(name: String, image_url: String, quantity: Int) {
          self.name = name
          self.image_url = image_url
          self.quantity = quantity
      }
  }
  </code></pre></li>
  <li><p><strong>ProductTableViewCell</strong> - This is UITableViewCell class</p>
  
  <pre><code>class ProductTableViewCell: UITableViewCell {
      @IBOutlet weak var productQuantity: UILabel!
      @IBOutlet weak var productName: UILabel!
      @IBOutlet weak var productImageView: UIImageView!
  }
  </code></pre></li>
  </ol>