解析:无法从 post 中检测到喜欢的 ID 以启用喜欢的图像
Parse: Cannot detect liked Id from a post to enable a Liked image
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)
}
}
// 以上部分是我的 IBAction 的委托方法,来自 tableviewcell 的 upvote 按钮单元格。
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)}
})
我想在用户已经喜欢 post 时显示活跃的喜欢的图像,尽管它能够显示,因为我在按下 upvote 按钮时在 IBAction 中启用了喜欢的图像。但不幸的是,它并没有在重新登录系统后显示一个人喜欢的post上的活跃点赞图像。
我认为您只需要对此产生一些想法。你可以尝试这样的事情。抱歉,我还不知道 Swift,但希望你能转换我的 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];
}
在 Swift 中执行类似操作的类似方法:
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)
}
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)
}
}
// 以上部分是我的 IBAction 的委托方法,来自 tableviewcell 的 upvote 按钮单元格。
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)}
})
我想在用户已经喜欢 post 时显示活跃的喜欢的图像,尽管它能够显示,因为我在按下 upvote 按钮时在 IBAction 中启用了喜欢的图像。但不幸的是,它并没有在重新登录系统后显示一个人喜欢的post上的活跃点赞图像。
我认为您只需要对此产生一些想法。你可以尝试这样的事情。抱歉,我还不知道 Swift,但希望你能转换我的 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];
}
在 Swift 中执行类似操作的类似方法:
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)
}