Swift 解析:无法从帖子中检测喜欢的Id以启用喜欢的图像
//上面的部分用于显示like count,它可以工作。Swift 解析:无法从帖子中检测喜欢的Id以启用喜欢的图像,swift,parse-platform,nsarray,Swift,Parse Platform,Nsarray,//上面的部分用于显示like count,它可以工作。 var showTopicLikeNumber = PFUser.query() showTopicLikeNumber.whereKey("liked", equalTo: topic.objectId) showTopicLikeNumber.findObjectsInBackgroundWithBlock({ (objects:[AnyObject]!,error:N
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中upvote按钮单元格的我的iAction的委托方法。
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)
}
}
当用户已经喜欢一篇文章时,我想显示一个活动的喜欢的图像,尽管它能够显示,因为我在iAction中按upvote按钮时启用了喜欢的图像。但不幸的是,在重新登录系统后,它并没有在帖子上显示人们喜欢的主动向上投票的图像。我想你只是需要一点概念火花。你可以试试这样的。对不起,我还不知道斯威夫特,但希望你能转换我的Obj-C来解决你的问题
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)}
})
并将下面的代码放入您的表格视图单元格中,以获得rowatinexpath
- (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];
}
在Swift中执行类似操作的类似方式:
//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];
}