title: dequeueReusableCellWithIdentifier creating a new cell instead of reusing one
body: <p>I use a <code>UITableView</code> inside a <code>UIViewController</code> with custom cells. However, when <code>dequeueReusableCellWithIdentifier</code> is called, it sometimes creates a new cell instead of reusing an existing one.</p>
  
  <p><strong>For example:</strong> When the first cell gets updated for the first time, a new cell is created instead of reusing the original cell.</p>
  
  <p>I have created a diagram to help better illustrate the problem. Each green rectangle is an action that should happen, and each red rectangle is one that shouldn't. Each arrow is a call of <code>dequeueReusableCellWithIdentifier</code>.</p>
  
  <p><a href="https://i.stack.imgur.com/ntR39.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ntR39.png" alt="diagram example"></a></p>
  
  <p>It's problematic when a <code>UIButton</code> is tapped (the action is called multiple times), and with a "swipe to delete" action, the cell which has been swiped can be covered by the new second cell, which makes it appear like the delete button is randomly disappearing.</p>
  
  <p>How can I solve this problem?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>code custom cell </p>
  
  <pre><code>class TableViewCell : UITableViewCell{
     @IBOutlet weak var buttonStop: UIButton!
     required init?(coder aDecoder: NSCoder) {
         super.init(coder: aDecoder)
         print("newCell")
         backgroundColor = color
     }
     override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
         super.init(style: style, reuseIdentifier: reuseIdentifier)
     }
     override func prepareForReuse() {
         super.prepareForReuse()
  
     }
     var color = UIColor().randomColor()
  
     }
  </code></pre>
  
  <p>code for extension randomColor </p>
  
  <pre><code>extension UIColor {
  func randomColor() -&gt; UIColor {
      let aRedValue = CGFloat(arc4random()) % 255 / 255
      let aGreenValue = CGFloat(arc4random()) % 255 / 255
      let aBlueValue = CGFloat(arc4random()) % 255 / 255
      return UIColor(red: aRedValue, green: aGreenValue, blue: aBlueValue, alpha: 1)
  }
  </code></pre>
  
  <p>code for cellForRow</p>
  
  <pre><code>override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell
  {
      let cell = tableView.dequeueReusableCellWithIdentifier("Cell",forIndexPath: indexPath) as! TableViewCell
      cell.buttonStop.tag = indexPath.row
      cell.buttonStop.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
      return cell
  }
  </code></pre>
  
  <p>buttonAction: </p>
  
  <pre><code>func buttonAction(sender: UIButton) {
      print("action for cell : \(sender.tag)")
  }
  </code></pre>
  
  <p>I reduced the code to the max , note after  further investigation the button printed the same text because of one of my mistake I write the message twice ^^'
  So the only problem remaining is for the "swipe to delete" gesture</p>