2015-09-16 101 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他沒有,但它看起來並不那麼棒。到目前爲止,我所能找到的唯一答案是刷新整個表格,但我不想這樣做。