我想防止已报告的评论被重新加载到我的评论表中。所以我做了一个removedIDs表:
var removedIDs = [Comment]()
我也有
var comments = [Comment] ()
然后我希望当您报告评论时,它会将其添加到列表中,这样:
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { let more = UITableViewRowAction(style: .default, title: "Report") { action, index in self.comments.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade) }; more.backgroundColor = UIColor.blue; self.removedIDs.append(comments[indexPath.row]); return [more]
}
然后我想在加载评论时我可以:
func loadComments(forPostId: String) {
let ref = self.ref.child("comments")
let thisPostRef = ref.child(forPostId)
thisPostRef.observeSingleEvent(of: .value, with: { snapshot in
let allComments = snapshot.children.allObjects as! [DataSnapshot]
for commentSnap in allComments {
let commenterUid = commentSnap.childSnapshot(forPath: "comment_by_uid").value as? String ?? "No uid"
let commentText = commentSnap.childSnapshot(forPath: "comment_text").value as? String ?? "No comment"
let aComment = Comment(id: commenterUid, text: commentText)
if removedIDs.contains(aComment){
print("skipped")
}
else {
self.comments.append(aComment)
print(commenterUid, commentText)
}
}
self.commentsTable.reloadData()
})
}
但我收到错误
Argument type 'Comment' does not conform to expected type 'Equatable'
上线:removedIDs.contain(aComment){
请注意,我的评论模型如下:
import Foundation
类评论{
var id:String
var text:String
init(id: String, text:String) {
self.id = id
self.text = text
}
static func parseComment(_ key:String, data:[String:Any]) -> Comment? {
if let text = data["text"] as? String{
return Comment(id: key, text: text)
}
return nil
}
}
如何实现这个功能?
最佳答案
此removedIDs.contain(aComment){
需要Equatable
一致性
class Comment : Equatable {
static func == (lhs: Comment, rhs: Comment) -> Bool {
return lhs.id == rhs.id
}
var id:Int!
}
关于ios - 防止重新加载已报告的评论,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57185445/