2015-04-15 60 views
-1
 var showTopicLikeNumber = PFUser.query() 
     showTopicLikeNumber.whereKey("liked", equalTo: topic.objectId) 

     showTopicLikeNumber.findObjectsInBackgroundWithBlock({ 
     (objects:[AnyObject]!,error:NSError!)->Void in 

     if (error == nil){ 
      let liked:NSArray = objects as NSArray 
      cell.upvoteButton.setTitle("\(liked.count)", forState: UIControlState.Normal) 
     } 

//以上的部分是用於顯示像數,和它的作品。解析:不能從後發現喜歡標識,以使喜歡的圖像

 func topicTableViewCellDidTouchUpvote(cell: TopicTableViewCell, sender: AnyObject) { 
     if PFUser.currentUser() != nil{ 

     let senderButton:UIButton = sender as UIButton 
     var topicLiked:PFObject =  
     timelineTopicData.objectAtIndex(senderButton.tag) as PFObject 

     println(topicLiked.objectId) 

     PFUser.currentUser().addUniqueObject(topicLiked.objectId, forKey: "liked") 
     PFUser.currentUser().save() 

     senderButton.setImage(UIImage(named:"icon-upvote-active"), forState: UIControlState.Normal) 
    } 

    else{ 
     performSegueWithIdentifier("loginTopicSegue", sender: self) 
    } 
    } 

//以上部分是我從tableviewcell給予好評鈕釦電池的IBAction爲的委託方法。

 var showTopicUpvoteEnable = PFQuery(className: "Topics") 
     showTopicUpvoteEnable.whereKey("objectId", equalTo:PFUser.currentUser().valueForKey("liked")) 

     showTopicUpvoteEnable.findObjectsInBackgroundWithBlock({ 
     (objects:[AnyObject]!,error:NSError!)->Void in 
     if error == nil{ 
     cell.upvoteButton.setImage(UIImage(named:"icon-upvote-active"), forState: UIControlState.Normal)} 
     else{ 
     cell.upvoteButton.setImage(UIImage(named: "icon-upvote"), forState: UIControlState.Normal)} 
       }) 

//我想表明的積極喜歡的圖像,當用戶已經喜歡上了一個帖子,雖然它能夠顯示,因爲我在IBAction爲使喜歡的圖像時,按下按鈕給予好評。但不幸的是,它不會在帖子重新登錄系統後喜歡的帖子上顯示活躍的upvote圖像。

回答

0

我想你只是需要圍繞這個概念的火花。你可以嘗試這樣的事情。對不起,我不知道斯威夫特,但希望你可以轉換我的Obj-C來解決你的問題。

- (void)didTapStarButtonAction:(UIButton *)button{ 

    ...  

    // check if current user already liked the post 
    if (![[object objectForKey:@"whoLiked"]containsObject:[PFUser currentUser].objectId]) { 

     //add the object ID for the cell we are liking to the array of liked items in the user class in parse 
     [[PFUser currentUser] addUniqueObject:object.objectId forKey:@"liked"]; 
     [[PFUser currentUser] saveInBackground]; 

     //add the user ID to the post that the user liked 
     [object addUniqueObject:[PFUser currentUser].objectId forKey:@"whoLiked"]; 
     [object saveInBackground]; 

    } else { 

     //remove the object ID for the cell we are liking to the array of liked items in the user class in parse 
     [[PFUser currentUser] removeObject:object.objectId forKey:@"liked"]; 
     [[PFUser currentUser] saveInBackground]; 

     //remove the user ID to the post that the user liked 
     [object removeObject:[PFUser currentUser].objectId forKey:@"whoLiked"]; 
     [object saveInBackground]; 

    } 

    [self.tableView reloadData]; 
} 

並把下面的代碼在你的的tableView的cellForRowAtIndexPath

//star 
UIButton *starButton = (UIButton*) [cell.contentView viewWithTag:kPAWCellStarButtonTag]; 

if ([[object objectForKey:@"whoLiked"]containsObject:[PFUser currentUser].objectId]) { 
    [starButton setImage:[UIImage imageNamed:@"pressedStar.png"] forState:UIControlStateNormal]; 
} else { 
    [starButton setImage:[UIImage imageNamed:@"unpressedStar.png"] forState:UIControlStateNormal]; 
} 
+0

如果這個回答你的問題,請接受它 – SanitLee

+0

解決了它...謝謝Sanit Lee –

0

類似的方式做這樣的事情在斯威夫特:

let user = PFUser.currentUser()?.username 

if object.objectForKey("whoLiked")?.containsObject(user!) == true { 
     startButton.setImage(UIImage(named: "pressedStar"), forState: UIControlState.Normal) 
    } else { 
     startButton.setImage(UIImage(named: "unpressedStar"), forState: UIControlState.Normal) 
}