【问题标题】:How to refresh table view such that index is updated too?如何刷新表视图以使索引也更新?
【发布时间】:2018-03-10 03:14:36
【问题描述】:

目前,我正在实施一个帖子系统,这样用户可以使用 firebase 点赞帖子,但只能点赞一次。帖子被点赞后,firebase 会更新日期,并且应用会根据点赞日期对帖子重新排序,因此

if changedPost.Revealed == "true" {

     changedPost.revealDate = value["revealedDate"] as? Double ?? 00000
     self.orderedPostList = self.postList.sorted(by: { $0.revealDate! > $1.revealDate! })
     if let newName = snapshot.childSnapshot(forPath: "information").value as? NSDictionary {

           changedPost.name = newName["posterName"] as? String ?? "Poster not found"
     }
     self.tableView.reloadData()       
 }  

点赞机制基于

 let location : CGPoint = sender.location(in: self.tableView)

        if let indexPath = tableView.indexPathForRow(at: location)?.row {

            let postIDLocation = postList[indexPath]
            let postIDPressed = postIDLocation.postID
            let numberOfRevealsPostNeeds = postIDLocation.revealsRequired
            let numberOfReveals = Int(postIDLocation.Reveals!)
            let revealReference = Database.database().reference().child("reveals").child(postIDPressed!)

            revealReference.observeSingleEvent(of: .value, with: { (snapshot) in
                let userID = Auth.auth().currentUser?.uid

                if snapshot.hasChild(userID!) {
                    print ("hello")
                }
                else {

                    revealReference.updateChildValues([userID!: "true"])
                    let postRef = Database.database().reference().child("posts").child(postIDPressed!)
                    let addedReveal = numberOfReveals + 1

问题是,用户点赞帖子后,它会移动到提要的顶部,这样一个新帖子就会占据该帖子曾经占据的位置。但是,用户现在无法喜欢那里的帖子,因为表格视图上的那个位置曾经被现在喜欢的帖子占据。当用户重新启动应用程序时,他们可以点赞占据曾经持有该帖子的帖子的位置的帖子。有没有办法在帖子重新排序后重新调整表视图索引?它们目前存储在字典中,如下所示:

orderedPostList = postList.sorted(by: { $0.revealDate! > $1.revealDate! })

我已尝试刷新表视图,但索引仍与错误的帖子相关联。基本上,这些帖子是从 firebase 获取的,然后根据他们喜欢的日期分类到字典中;但是,当点赞日期发生变化时,帖子位置会发生变化,但占据点赞帖子之前位置的新帖子无法被点赞,直到应用重新启动并刷新索引。

【问题讨论】:

  • 这看起来不像是展示代码的好方法。
  • @El Tomato 是什么意思?我应该重新组织问题吗?我认为这是与该问题唯一相关的代码。
  • 您缺少三个或更多大括号。
  • @ElTomato 我排除了那些,只包括代码的相关部分。如果我将代码继续到大括号,还会发生其他事情。谢谢你的建议。

标签: swift uitableview firebase indexing tableview


【解决方案1】:

问题在于类似机制使用的是无序字典。当我让 like 机制的手势识别器使用有序字典中的索引时,like 会转到正确的帖子。

【讨论】:

    猜你喜欢
    • 2014-04-29
    • 2014-10-09
    • 1970-01-01
    • 2014-06-24
    • 1970-01-01
    • 2012-07-01
    • 2011-12-26
    • 2020-09-05
    • 1970-01-01
    相关资源
    最近更新 更多