title: Getting duplicate tableview cells after first time
body: <p>In my app, there is an add button, when clicked will add a new tableviewcell. However the problem is I am getting duplicate cells after the first time. Essentially, I am getting 2 duplicates when I click second time, 3 on third and so on.... Please see the images for the screenshot:
  <a href="https://i.stack.imgur.com/4qDPj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4qDPj.png" alt="Screen 1"></a></p>
  
  <p>The above image is the first screen. Clicking '+' will add a new cell, which looks like the below images:
  <a href="https://i.stack.imgur.com/xiCy6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xiCy6.png" alt="Screen 2: Create cell"></a></p>
  
  <p>Now after saving, 
  <a href="https://i.stack.imgur.com/uZo4U.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uZo4U.png" alt="Screen 3:after save"></a></p>
  
  <p>Now if I go out of this view and come back, click add again and create a new cell, it gives me duplicates as mentioned in the above paragraphs (that is, 2 for second time, 3 for third time and so on...). 
  Here is the screenshot: <a href="https://i.stack.imgur.com/bsg3g.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bsg3g.png" alt="enter image description here"></a></p>
  
  <p>Here is the code I am using:</p>
  
  <pre><code> @IBAction func addBasket(_ sender: UIButton) {
  
      let prefs         = UserDefaults.standard
      let status        = prefs.string(forKey: "status")
  
      if(status != nil){
  
          if( status == "pending"){
  
              if(BasketItemList.count &lt; 5 ) {
  
                  self.addpost()
  
              } else {
  
  
                  let alert = SCLAlertView()
                  alert.showWarning("Basket", subTitle: "Upgrade to add more")
  
              }
  
  
  
          } else {
  
              if(BasketItemList.count &lt; 50 ) {
  
                  self.addpost()
  
              } else {
  
                  let alert = SCLAlertView()
                  alert.showWarning("Basket", subTitle: "Upgrade to add more")
  
              }
  
  
  
          }
  
  
      }
  
  
  
  }
  
  
  func addpost() {
  
      let appearance = SCLAlertView.SCLAppearance(
          showCloseButton: false
      )
  
      let alert = SCLAlertView(appearance : appearance)
      let txt = alert.addTextField("Enter name")
      alert.addButton("Save") {
  
          if txt.text?.characters.count != 0 {
  
              let basketname : String = txt.text!
  
              let userID = FIRAuth.auth()?.currentUser?.uid
  
              let postitem : [String :AnyObject] = ["userid" : userID! as AnyObject , "basketname" : basketname as AnyObject ]
  
              let dbref = FIRDatabase.database().reference()
              dbref.child("BasketList").childByAutoId().setValue(postitem)
  
              self.Basketdata2()
  
  
  
              let appearance = SCLAlertView.SCLAppearance(
                  kDefaultShadowOpacity: 0,
                  showCloseButton: false
              )
  
              let alertView = SCLAlertView(appearance: appearance)
              alertView.showTitle(
                  "Saved", // Title of view
                  subTitle: "", // String of view
                  duration: 2.0, // Duration to show before closing automatically, default: 0.0
                  completeText: "Done", // Optional button value, default: ""
                  style: .success, // Styles - see below.
                  colorStyle: 0xA429FF,
                  colorTextButton: 0xFFFFFF
              )
  
  
  
  
          } else {
  
  
              let alert = SCLAlertView()
              alert.showError("Oops!", subTitle: "Basket name should not be empty")
  
              self.tableviewbasket.reloadData()
  
          }
  
      }
      alert.addButton("Cancel"){
  
  
      }
      alert.showEdit("Add basket", subTitle: "Please enter your basket name")
  
  
  
  
  }
  
  func Basketdata2() {
      HUD.show(.labeledProgress(title: "Loading...", subtitle: ""))
  
      let databaseref = FIRDatabase.database().reference()
      var userID = FIRAuth.auth()?.currentUser?.uid
  
      if userID == nil {
  
          userID = userfbid
      }
  
      databaseref.child("BasketList").queryOrdered(byChild: "userid").queryEqual(toValue: userID!).observeSingleEvent(of: .value, with: { (snapshot) in
  
          if snapshot.exists() {
  
              self.tableviewbasket.backgroundView = nil;
              HUD.hide()
  
          }  else {
  
              HUD.hide()
  
  
              self.tableviewbasket.setContentOffset(CGPoint(x : 0, y: -98), animated: true)
  
  
              if (self.BasketItemList.count == 0) {
  
                  // tableView is empty. You can set a backgroundView for it.
                  let label = UILabel(frame: CGRect(x: 5, y: 0, width: self.tableviewbasket.bounds.size.width, height:self.tableviewbasket.bounds.size.height))
                  label.text = "The best preparation for tomorrow \n is doing your best today.\n Please create your first basket."
                  label.textColor = UIColor.black;
                  label.textAlignment = .center
                  label.numberOfLines = 4
                  label.sizeToFit()
                  label.font = UIFont(name: "AvenirNext-Regular", size: 16.0)
                  self.tableviewbasket.backgroundView = label;
                  self.tableviewbasket.separatorStyle = .none;
  
              }
  
          }
  
      })
  }
  
  
    func Basketdata() {
  
      HUD.show(.labeledProgress(title: "Please wait...", subtitle: ""))
  
      self.BasketItemList.removeAll()
      self.Basketid.removeAll()
  
      let databaseref = FIRDatabase.database().reference()
      let userID = FIRAuth.auth()?.currentUser?.uid
  
      databaseref.child("BasketList").queryOrdered(byChild: "userid").queryEqual(toValue: userID!).observeSingleEvent(of: .value, with: { (snapshot) in
  
          if snapshot.exists() {
  
              databaseref.child("BasketList").queryOrdered(byChild: "userid").queryEqual(toValue: userID!).observe(.childAdded, with: {
                  (snapshot) in
  
  
  
                  if let dictionary = snapshot.value as? [String : AnyObject] {
  
  
                      let basketitem = BasketList(text : "")
                      basketitem.setValuesForKeys(dictionary)
  
                      self.BasketItemList.append(basketitem)
                      self.Basketid.append(snapshot.key)
                      DispatchQueue.main.async {
  
                          if !self.BasketItemList.isEmpty {
  
                              HUD.hide()
                              self.tableviewbasket.reloadData()
  
                          }
  
                      }
  
  
                  } else {
  
                      self.tableviewbasket.reloadData()
                      HUD.hide()
                  }
  
  
              })
  
  
  
  
          }  else {
  
              if (self.BasketItemList.count == 0) {
  
                  // tableView is empty. You can set a backgroundView for it.
                  let label = UILabel(frame: CGRect(x: 5, y: 0, width: self.tableviewbasket.bounds.size.width, height:self.tableviewbasket.bounds.size.height))
                  label.text = "The best preparation for tomorrow \nis doing your best today"
                  label.textColor = UIColor.black;
                  label.textAlignment = .center
                  label.numberOfLines = 2
                  label.sizeToFit()
                  label.font = UIFont(name: "AvenirNext-Regular", size: 16.0)
                  self.tableviewbasket.backgroundView = label;
                  self.tableviewbasket.separatorStyle = .none;
  
              }
  
  
              HUD.hide()
  
  
          }
  
      })
  
  
  
  
  }
  </code></pre>
  
  <p>Can someone help me understand what is wrong with my code? Thanks!</p>
  
  <p>Edit: I have referred this thread without any luck: <a href="https://stackoverflow.com/questions/36707823/getting-duplicate-cells-with-uitableviewcontroller-cellforrowatindexpath">Getting duplicate cells with UITableViewController cellForRowAtIndexPath</a></p>
  
  <p>Edit 2: Also, when I come out of that view and go to the same view, the duplicate values are vansihed.</p>
  
  <p>Edit 3: Tried the answer without any success.</p>