BizDev
BizDev

Reputation: 481

Getting duplicate tableview cells after first time

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: Screen 1

The above image is the first screen. Clicking '+' will add a new cell, which looks like the below images: Screen 2: Create cell

Now after saving, Screen 3:after save

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: enter image description here

Here is the code I am using:

 @IBAction func addBasket(_ sender: UIButton) {

    let prefs         = UserDefaults.standard
    let status        = prefs.string(forKey: "status")

    if(status != nil){

        if( status == "pending"){

            if(BasketItemList.count < 5 ) {

                self.addpost()

            } else {


                let alert = SCLAlertView()
                alert.showWarning("Basket", subTitle: "Upgrade to add more")

            }



        } else {

            if(BasketItemList.count < 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()


        }

    })




}

Can someone help me understand what is wrong with my code? Thanks!

Edit: I have referred this thread without any luck: Getting duplicate cells with UITableViewController cellForRowAtIndexPath

Edit 2: Also, when I come out of that view and go to the same view, the duplicate values are vansihed.

Edit 3: Tried the answer without any success.

Upvotes: 0

Views: 1108

Answers (1)

dahiya_boy
dahiya_boy

Reputation: 9503

Follow below steps:

  1. When you'r getting data from firebase db, first remove your array all objects that you'r using in the cellForRow method. In your case i think it should be array of Bucket (not sure).

  2. Assign data to your object

  3. reload tableview.

Reason of replication of data.

let your bucket have 2 values and it is stored inDB. When you fetch data from DB it gives you all the values i.e. 1,2,3. Now you adds these now your array will be 1,2,1,2,3.

Thats what happening in your case.

Upvotes: 1

Related Questions