【问题标题】:Like button feature not working through parse像按钮功能无法通过解析工作
【发布时间】:2015-11-01 22:20:46
【问题描述】:

我正在尝试通过解析在我的应用程序上实现类似 facebook 或 instagram 的点赞按钮功能。我尝试在下面使用此代码并且它有效。当用户点击对象上的按钮(或在我的情况下为消息)时,like 增加 1 点。然而,当用户退出应用程序并启动它时,他们可以再次喜欢同一个对象,这意味着他们可以随意喜欢多次。我是否需要在此代码中编辑某些内容或尝试完全不同的方法?

@IBAction func likeButton(sender: UIButton) {
    sender.enabled = false
    sender.userInteractionEnabled = false
    sender.alpha = 0.5

    //get the point in the table view that corresponds to the button that was pressed
    //in my case these were a bunch of cells each with their own like button
    let hitPoint = sender.convertPoint(CGPointZero, toView: self.tableView)
    let hitIndex = self.tableView.indexPathForRowAtPoint(hitPoint)
    let object = objectAtIndexPath(hitIndex)

    //this is where I incremented the key for the object
    object!.incrementKey("count")
    object!.saveInBackground()

    self.tableView.reloadData()
    NSLog("Top Index Path \(hitIndex?.row)")
}

【问题讨论】:

  • 也许您可以尝试使用数组或关系来跟踪每个用户喜欢的所有帖子,然后在创建帖子单元格时,检查数组或关系是否已经包含该帖子.如果是,请阻止用户再次点赞。
  • 这是我在 Parse 上做的事情吗?

标签: ios swift parse-platform pfquery


【解决方案1】:

这是我解决问题的方法:

首先,我会将按钮禁用到它自己的功能中,如下所示:

func disableButton(button: UIButton){
    button.enabled = false
    button.userInteractionEnabled = false
    button.alpha = 0.5
}

(当用户按下like按钮时,它会像这样被禁用:)

@IBAction func likeButton(sender: UIButton) {
    disableButton(sender)

    //get the point in the table view that corresponds to the button that was pressed
    //in my case these were a bunch of cells each with their own like button
    let hitPoint = sender.convertPoint(CGPointZero, toView: self.tableView)
    let hitIndex = self.tableView.indexPathForRowAtPoint(hitPoint)
    let object = objectAtIndexPath(hitIndex)

    //this is where I incremented the key for the object
    object!.incrementKey("count")
    object!.saveInBackground()

    self.tableView.reloadData()
    NSLog("Top Index Path \(hitIndex?.row)")
}

然后我会为用户创建他们喜欢的帖子的属性,这是这些帖子的objectIds 的字符串数组。 (由于用户最终可能会喜欢很多帖子,因此您确实应该使用关系,但数组更容易理解。PFObjects 之间关系的文档是 here。)然后,在为每个帖子创建单元格时,@ 987654326@ 是您正在创建的单元格,post 是列表中的当前帖子,cell.likeButton 是单元格中的点赞按钮:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
    let cell = YourCell()
    let post = posts[indexPath.row]
    if (PFUser.currentUser!["likedPosts"] as! [String]).contains(post.objectId){
        cell.disableButton(cell.likeButton)
    }
    //Setup rest of cell

【讨论】:

  • 没有。这只是一个正常的功能。
  • 您是否还在单元格中为 cell.disableButton 创建了一个插座?
  • 如果将 disableButton 添加到单元格子类中,点符号将访问它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-10
  • 2012-12-28
  • 1970-01-01
相关资源
最近更新 更多