swift - 当快照侦听器生效时,Tableview Like 按钮会重复
问题描述
所以基本上当点击like按钮时,它假设改变tableview中的likes数量,但是发生的事情是它在tableview数组的底部创建了一个重复的单元格,并且它使用新的likes数量,我做错了,但我不知道如果有更好的方法可以做到这一点,我会欣赏它向我展示
import UIKit
import Firebase
class motivationviewcontroller : UIViewController,UITableViewDataSource ,UITableViewDelegate{
var motivationThoughts = [motivationDailyModel]()
var Mous = motivationDailyModel()
var tableview : UITableView!
override func viewDidLoad() {
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()
//listener()
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 { querySnapshot, error in
guard let snapshot = querySnapshot else {
print("Error fetching snapshots: \(error!)")
return
}
snapshot.documentChanges.forEach { diff in
if (diff.type == .added) { // this line means if the chage that happened in the document was equal to added something
let data = diff.document.data()
print("we have\(snapshot.documents.count) documents in this array")
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)
}
//===== //=====
if (diff.type == .modified) {
print("Modified data: \(diff.document.data())")
let newdata = diff.document.data()
let dailyMotivationID = newdata["objectID"] as! String
let dailymotivationTitle = newdata["Motivation title"] as! String //calls the data thats heald inside of motivation title in firebase
let dailyMotivationScripture = newdata["daily motivation scripture"] as! String //calls the data thats heald inside of Motivation script in firebase
let dailyMotivationNumberOfLikes = newdata["Number of likes in daily motivation post"]as! Int
let MdataModel = motivationDailyModel(RealMotivationID: dailyMotivationID, RealmotivationTitle: dailymotivationTitle, RealmotivationScrip: dailyMotivationScripture, RealmotivationNumberOfLikes: dailyMotivationNumberOfLikes)
self.motivationThoughts.append(MdataModel)
// here you will receive if any change happens in your data add it to your array as you want
}
DispatchQueue.main.async {
self.tableview.reloadData()
}
}
}
}
解决方案
我不知道是否loaddailymotivation()
在 之后再次调用viewDidLoad
,但是在这种方法中,您正在执行self.motivationThoughts.append(MdataModel)
的操作应该与您所说的完全一致。
我认为您需要一种方法来识别数组中需要修改的项目,并替换/修改它。如果您添加一个新对象,您的表格中就会多出一个元素
推荐阅读
- solr - 无法联系您的 Apache Solr 服务器
- android - 找不到服务托管接口 android.hardware.radio.config@1.0::IRadioConfig/default
- c# - 在 C# 中创建列表列表
- vim - 如何将变量扩展为高亮命令的参数?
- wordpress - 错误:cURL 错误 28:使用 docker 中的 nginx 反向代理解决 10000 毫秒后超时(http_request_failed)
- javascript - bootstrap-4 卡没有填充到行底部的高度
- ios - 我的设备 (ios) 上的测试应用未显示我的应用内购买
- javascript - 如何将获取响应保存为输入值
- php - 如何获取会话数组的索引?- cakephp
- linked-list - 在 OCaml 中拆分列表