title: How to compare the data from current cell with its previous cell inside TableView Swift 4?
body: <p>I have a JSON is look some thing like this : </p>
  
  <pre><code>"product": [
      {
          "product_id": 471,
          "info": "123456",
      },
      {
          "product_id": 471,
          "info": "356697456",
      },
      {
          "product_id": 472,
          "info": "1432",
      },
      {
          "product_id": 473,
          "info": "4321",
      },
  ]
  </code></pre>
  
  <p>I want to set my TableView to look something like this image below : </p>
  
  <p><a href="https://i.stack.imgur.com/hAheG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hAheG.png" alt="enter image description here"></a></p>
  
  <p>what I want is:</p>
  
  <ol>
  <li><p>If the first cell in the TableView I want the Product 1 (in Red color) shown.</p></li>
  <li><p>If the second cell's <code>product_id</code> is same with it previous cell's <code>product_id</code> then Product 1 is no longer shown, it disappears.</p></li>
  <li><p>Since the 3rd cell's <code>product_id</code> is not same with previous cell (second cell), so the red label Product 2 is shown up.</p></li>
  <li><p>Same go to <code>Product 3</code> and the rest of the cell in the <code>TableView</code></p></li>
  </ol>
  
  <p>What I already tried:</p>
  
  <p>In order to achieve this,I need to get the indexPath inside <code>cellAtRow</code> delegate,so I compare each cell's <code>product_id</code> with the previous one,and then control the logic inside.</p>
  
  <p>Here is my code</p>
  
  <pre><code>func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
          let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) as! MyCell
  
          let thisIndexPath = indexPath.row
          if thisIndexPath - 1 &gt; -1 {
  
              let previousProductId = self.productItem[thisIndexPath - 1].productId
              let thisProductId = self.productItem[thisIndexPath].productId
  
              if  previousProductId == thisProductId {
  
                  cell.productLabel.isHidden = true
                  cell.productHeight.constant = 0
                  cell.productnameTopContraint.constant = 0
                  cell.productnameBottomContraints.constant = 0
              }else {
                  cell.productnameLabel.isHidden = false
  
              }
          }else{
              cell.productnameLabel.isHidden = false
          }
  
          cell.item = selfProductItem[indexPath.row]
          return cell
      }
  }
  </code></pre>
  
  <p>But now the problem is:</p>
  
  <p>-- When the <code>TableView</code> first launch,the UI display like that I shown above,but when I start scrolling,all the cell's <em>Product</em> label(in red color) is gone,although the <code>product_id</code> is not same with the previous cell's <code>product_id</code>.</p>
  
  <p>-- When I scroll back to the first cell,the Product Label(In red color) is gone as well.Which means the UI is only right at first launch of the screen,which is not persistent.</p>
  
  <p>So my question is:</p>
  
  <ol>
  <li><p>What is the correct way to compare data from current cell to the previous cell?</p></li>
  <li><p>Is it right to do the comparison inside <code>cellForRowAt</code> delegate method?If not,where should I do this? </p></li>
  </ol>