2015-09-16 140 views
0

在我的社交媒体应用程序我有一个类似按钮...刷新行不工作

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行之类的标签不改变某种原因,也这是我设置按钮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" 

有谁知道可能会发生什么?谢谢!只要告诉我你是否需要更多信息! (:做

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

我要接受这个当我一个答案

+1

另外,您不需要所有这些'dispatch_async's您显示的所有代码都将在主队列中执行 – Paulw11

回答

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") 

尝试突破它。

0

我能够把它的方法

if success == true { 
} 

来解决问题,大声说,但如果有人知道如何,我很想听听如何停止粗糙的动画

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

因为即使有t他没有,但它看起来并不那么棒。到目前为止,我所能找到的唯一答案是刷新整个表格,但我不想这样做。