Error in UITableview when pull down to refresh, all data repeating itself. Can any one help using Swift3 , Xcode8

85 views Asked by At

I have data coming from database in my UITableView, when I try to pull down to make refresh all data in my tableview repeat itself and when I am trying to put a new data in database and making refresh nothing happens.

This is my code

 class MyTable: UITableViewController {

   var refresh = UIRefreshControl()
   @IBAction func SharaOn(_ sender: UIButton) {
   }
   var mydata = [TestTable]()
   let backendless = Backendless()

   override func viewDidLoad() {
   super.viewDidLoad()
   refresh = UIRefreshControl()
   refresh.addTarget(self, action: #selector (MyTable.pullDown), for: .valueChanged)

      tableView.addSubview(refresh)

      }

    override func viewDidAppear(_ animated: Bool) {
         loaddatawithquery()
   }


     override func numberOfSections(in tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of  sections
    return 1
     }

     override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
       // #warning Incomplete implementation, return the number of rows
       return mydata.count
      }


     override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

      if let cell = tableView.dequeueReusableCell(withIdentifier: "mycell") as? MyTableCell{



        let ImgURL = URL(string : self.mydata[(indexPath as NSIndexPath).row].ImgURL!)
       let Desc = self.mydata[(indexPath as NSIndexPath).row].Desc!
        cell.MyText.text = Desc
        cell.mainImage.sd_setImage(with: ImgURL)

        return cell

       }else{

        let cell = MyTableCell()
        let ImgURL = URL(string : self.mydata [(indexPath as NSIndexPath).row].ImgURL)
         let Desc = self.mydata[(indexPath as NSIndexPath).row].Desc

        cell.mainImage.sd_setImage(with: ImgURL)

        cell.MyText.text = Desc!

        return cell
        }


        }


         func updateLikes() {
            let LikeBu = TestTable()
             let updaterecord =       Backendless.sharedInstance().data.of(TestTable.ofClass())


              LikeBu.LikeNo = 1
              updaterecord?.save(LikeBu, response: { (result) in
                  print("udateed successfully \(result)")
                   }, error: { (Fault) in
                   print("EROrrrrrrrrrr")
               })


             }
         func loaddatawithquery(){


    _ = "ImgURL"
    _ = "Desc"
    let dataQuery = BackendlessDataQuery()
    dataQuery.queryOptions.pageSize=50

   // dataQuery.whereClause = whereClause

       backendless.data.of(TestTable.ofClass()).find(dataQuery,response: {(result: BackendlessCollection?) -> Void in
         let data = result?.getCurrentPage()

    for obj in data! as! [TestTable] {

            self.mydata.append(obj)

        //print("&&&&&&&hhhjjjhhvkhkh \(obj.Desc!) &&&&&&&&&&&&&&&&&&")


        self.tableView.reloadData()


        }
        },
        error: { (fault: Fault?) -> Void in

let alert = UIAlertController(title: "info", message:"يرجى الاتصال بالانترنيت", preferredStyle: .alert)

alert.addAction(UIAlertAction(title: "OK", style: .default) { _ in

                                         })

self.present(alert, animated: true){}


    })


}
@IBAction func likebutton(_ sender: AnyObject) {

   // updateLikes()

}


func pullDown() {


    for  data in mydata   {

        self.mydata.append(data)
    }

    self.refresh.endRefreshing()

}





 }
2

There are 2 answers

5
Saheb Roy On BEST ANSWER

As far as i can understand, its because of this code here

for  data in mydata   {

    self.mydata.append(data)
}

Here actually you are iterating the data inside the tableview, and also adding the data already there, and also fetching new data, hence in the actual array self.mydata, there are 2 sets of similar objects. I think a better approach would be if you can just append the new data after the query or you can empty the array and then load the whole data from your database.

Let me know.

2
Annie Gupta On

You need to update your code like this:-

func pullDown() {

   var temp = [TestTable]()

    for  data in mydata   {
        temp.append(data)
    }
    mydata.removeAll()
    mydata = temp
    self.tableView.reloadData()
    self.refresh.endRefreshing()

}