为什么每次重新加载tableview时都会有重复项?

时间:2020-04-18 02:54:58

标签: swift firebase uitableview google-cloud-firestore

因此,我在表视图单元格中单击了一个喜欢的按钮。每次点按它都会更新特定帖子的点赞次数的值。问题是,当它更新并且snapshotListener生效时,它会添加数据的另一个副本,因此我将有重复项,例如,当点按了“赞”按钮时,我有一个帖子有100个赞,我将有2个同一篇博文中,有100个赞,另一个有101个赞。

 import UIKit
 import Firebase



class motivationviewcontroller : UIViewController,UITableViewDataSource ,UITableViewDelegate{


var motivationThoughts = [motivationDailyModel]()
var Mous = motivationDailyModel()
var tableview : UITableView!

override func viewDidLoad() {
    print("madicc")

    print("the user logged in is \( String(describing: Auth.auth().currentUser?.email))")

    tableview =  UITableView(frame: view.bounds, style: .plain)
           tableview.backgroundColor = UIColor.white
           view.addSubview(tableview)


    var layoutGuide : UILayoutGuide!
    layoutGuide = view.safeAreaLayoutGuide

    let cellNib = UINib(nibName: "dailyMotivationTableViewCell", bundle: nil)
    tableview.register(cellNib, forCellReuseIdentifier: "DailyThoughtCELL")
    tableview.leadingAnchor.constraint(equalTo: layoutGuide.leadingAnchor).isActive = true
    tableview.topAnchor.constraint(equalTo: layoutGuide.topAnchor).isActive = true
    tableview.trailingAnchor.constraint(equalTo: layoutGuide.trailingAnchor).isActive = true
    tableview.bottomAnchor.constraint(equalTo: layoutGuide.bottomAnchor).isActive = true


    tableview.dataSource = self
    tableview.delegate = self


    loaddailymotivation()
    self.tableview.reloadData()


}

override func viewDidAppear(_ animated: Bool) {
  //loaddailymotivation()
    self.tableview.reloadData()

}




func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    motivationThoughts.count
   }

   func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "DailyThoughtCELL", for: indexPath) as? dailyMotivationTableViewCell

        cell!.generateCellsforDailymotivation(_MotivationdataMODEL: self.motivationThoughts[indexPath.row])
        return cell!
   }


          func loaddailymotivation() {

    FirebaseReferece(.MotivationDAILY).addSnapshotListener { (snapshot, error) in

        if (error != nil) {
            print(error!.localizedDescription)
        } else{

            guard let snapshot = snapshot else {return}

            for allDocuments in snapshot.documents {

                let data = allDocuments.data()

                let dailyMotivationID =  data ["objectID"] as! String

                let dailymotivationTitle = data["Motivation title"] as! String //calls the data thats heald inside of motivation title in firebase

                let dailyMotivationScripture = data["daily motivation scripture"] as! String //calls the data thats heald inside of Motivation script in firebase

                let dailyMotivationNumberOfLikes = data["Number of likes in daily motivation post"]as! Int

                let MdataModel = motivationDailyModel(RealMotivationID: dailyMotivationID, RealmotivationTitle: dailymotivationTitle, RealmotivationScrip: dailyMotivationScripture, RealmotivationNumberOfLikes: dailyMotivationNumberOfLikes)

                self.motivationThoughts.append(MdataModel)
                self.tableview.reloadData()

          }
        }

     }
   }

2 个答案:

答案 0 :(得分:2)

您能否在附加数组之前尝试删除数组中的所有项目

self.motivationThoughts.removeAll() 
self.motivationThoughts.append(MdataModel)
self.tableview.reloadData()

答案 1 :(得分:2)

您正在正确地从Firebase加载数据。

但是,在self.motivationThoughts.append(MdataModel)之前,您需要清除以前的数据,否则它将只是将新数据附加到以前的数据中,我相信这可以解释您所看到的内容。

self.motivationThoughts.removeAll()之类的东西可以做到。

相关问题