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)
}
// The above part is for displaying like count, and it works.
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)
}
}
// The above part is for delegate method of my IBAction of upvote button cell from tableviewcell.
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)}
})
I want to show an active liked image when a user already liked a post, though it is able to show since I enable a liked image in IBAction when press the upvote button. But unluckily, it does not display the active upvote image on the post that one liked after one re-login into the system.
I think you just need a spark of concept around this. You may try something like this. Sorry I don't know Swift yet but hopefully you can convert my Obj-C to solve your problem.
- (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];
}
And put the below code in your 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];
}