【问题标题】:Reload row at indexpath not working在索引路径重新加载行不起作用
【发布时间】:2015-09-16 23:38:05
【问题描述】:

在我的社交媒体应用程序中,我有一个赞按钮...

var likers = [String]()

func like(sender: UIButton) {

    var buttonPosition: CGPoint = sender.convertPoint(CGPointZero, toView: self.table)

    var indexPath: NSIndexPath = self.table.indexPathForRowAtPoint(buttonPosition)!

    testConnection()

    var post = posts[indexPath.row]

    if sender.currentTitle == "Unlike" {

        dispatch_async(dispatch_get_main_queue()) {
            self.table.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
        }

        sender.enabled = false

        post.removeObject(PFUser.currentUser()!.objectId!, forKey: "likers")
        post.saveInBackgroundWithBlock({ (success, error) -> Void in
            if success == true {
                sender.enabled = true
            }

            if error != nil {
                sender.enabled = true
            }
        })

    } else {
        dispatch_async(dispatch_get_main_queue()) {
            self.table.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
        }

        sender.enabled = false

        dispatch_async(dispatch_get_main_queue()) {

            post.addUniqueObject(PFUser.currentUser()!.objectId!, forKey: "likers")
            post.saveInBackgroundWithBlock({ (success, error) -> Void in
                if success == true {
                    sender.enabled = true


                }

                if error != nil {
                    sender.enabled = true
                }

            })

        }
    }

}

由于某种原因,当我在 indexPath 重新加载行时,like 标签也没有改变,这也是我在 cellForRowAtIndexPath 中设置按钮的地方...

dispatch_async(dispatch_get_main_queue()) {
            if PFUser.currentUser()?.objectId != nil {
                if (post["likers"] as! NSMutableArray).containsObject(PFUser.currentUser()!.objectId!) {
                    postCellObj.likeButton.setTitle("Unlike", forState: .Normal)

                } else {
                    postCellObj.likeButton.setTitle("Like", forState: .Normal)

                }

            } else {
                postCellObj.likeButton.setTitle("Like", forState: .Normal)

            }
        }

        postCellObj.numberOfLikes.text = ((post["likers"] as! [String]).count - 1).description + " Likes"

有人知道会发生什么吗?谢谢!如果您需要更多信息,请告诉我! (:

【问题讨论】:

  • 顺便说一句,您不需要所有这些dispatch_asyncs 您显示的所有代码都将在主队列上执行

标签: ios iphone swift uitableview parse-platform


【解决方案1】:

如果我正确阅读了您的代码,则您在重新加载表格视图单元格后删除了您的对象,而您正在重新加载您的单元格 - 标题仍然是“不一样”,因为您的 objectId 仍然存在于您的数组中:

(1)

if sender.currentTitle == "Unlike" {
    dispatch_async(dispatch_get_main_queue()) { 
    self.table.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
} 
...

(2)

if (post["likers"] as! NSMutableArray).containsObject(PFUser.currentUser()!.objectId!) {
    postCellObj.likeButton.setTitle("Unlike", forState: .Normal)
}

(3)

...

post.removeObject(PFUser.currentUser()!.objectId!, forKey: "likers")

尝试断点。

【讨论】:

    【解决方案2】:

    我能够通过将其放入方法中来解决问题

    if success == true { 
    }
    

    通过这样做

    sender.title = "Like" //Or unlike for the else statement
    

    当我大声回答时,我会接受这个作为答案,但如果有人知道怎么做,我很想听听如何停止那种粗暴的动画

    table.reloadRowsAtIndexPaths([indexPath], withRoAnimation: .None)
    

    因为即使使用 .None 它仍然看起来不太好。到目前为止,我能找到的唯一答案是刷新整个表格,但我不想这样做。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-12
      • 2015-06-02
      相关资源
      最近更新 更多