【问题标题】:Parse: Cannot detect liked Id from a post to enable a Liked image解析:无法从帖子中检测到喜欢的 ID 以启用喜欢的图像
【发布时间】:2015-04-15 03:43:22
【问题描述】:
        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 button cell 的委托方法。

       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 按钮时。但不幸的是,它不会在一个人重新登录系统后点赞的帖子上显示活跃的点赞图片。

【问题讨论】:

    标签: swift parse-platform nsarray


    【解决方案1】:

    我认为您只需要围绕这一点提出一些概念。你可以试试这样的。抱歉,我还不知道 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];
    }
    

    【讨论】:

      【解决方案2】:

      在 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)
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-04
        • 2020-11-23
        • 2020-11-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多