【问题标题】:Implementing like and unlike functionality in Swift using Parse使用 Parse 在 Swift 中实现相似和不同的功能
【发布时间】:2016-03-09 02:46:39
【问题描述】:

我正在尝试使用 Swift 2.0、Storyboard 和 Parse 在我的 iOS 应用程序中实现喜欢/不喜欢的功能,用户可以喜欢/不喜欢其他用户或他们自己创建的帖子 - 就像 Instagram、Facebook 和其他社交应用程序一样。

我在 Storyboard 中有一个按钮连接到一个名为 IBOutletlikeButton 和一个名为 IBActionlikeButtonTapped

我相信cellForRowAtIndexPath 方法也参与了正确实现此功能。

我认为我对下面代码的 cmets 中需要发生的事情有正确的想法,但是,我不知道如何检查特定帖子是否被喜欢。 如何检查帖子是否被点赞以便我可以切换likeButton 图片、增加/减少likeCount 以及添加/删除当前用户与帖子之间的关系用户喜欢的。

另外,对于标准的类似/不类似功能,我是否采用“正确”(传统)方法?我很想听听您的反馈。感谢您的宝贵时间和帮助!

class TimelineFeedViewController: PFQueryTableViewController {
    var userLike = PFUser.currentUser()?.relationForKey("likes")

    @IBAction func likeButtonTapped(sender: UIButton) {
        // The following code has errors - for example, `object` is an unresolved 
        // identifier (it's supposed to be a PFObject that holds a post at a specific 
        // indexPath)
        // Also, I cant access the likeButton for some reason. Only when I do
        // cell.likeButton in `cellForRowAtIndexPath`.
        // If the button is liked already (it's filled)
        // Toggle likeButton image to UNfilled version
        // "if likeButton isLiked == true" below is pseudocode of what I am trying to do
        if likeButton isLiked == true {
            let image = UIImage(named: "likeButtonUnfilled")
            cell.likeButton.setImage (image, forState: UIControlState)

            // Decrement the likeCount
            object!.decrementKey("count")

            // Remove the relation bet user and post
            self.userLike?.removeObject(object!)
        } else {
            // the button is NOT liked already (it's not filled)
            // toggle the image to the filled version
            let image = UIImage(named: "likeButton")
            cell.likeButton.setImage (image, forState: UIControlState)

            // Increment the likeCount
            object!.incrementKey("count")

            // Add the relation bet. user and post
            self.userLike?.addObject(object!)
        }

        object!.saveIngBackground()
        PFUser.currentUser()?.saveInBackground()

        self.tableView.reloadData()
    }
}

【问题讨论】:

  • UITableViewCell 中有按钮??
  • @SaqibOmer 我在一个单独的 Swift 类文件中实现了 IBOutlets,如 likeButton 等,该文件是 UITableViewCell 的子类
  • 增量和减量通常最好在云代码中完成。在关系中存储喜欢就可以了。
  • @Wain 我的增量和减量代码行(即:.incrementKey("count"))正在更新 Parse 云中的计数列(如计数)...
  • @Wain 另外,当我尝试打印出关系 userLike 时,我得到类似:“ userLike: Optional( PostClass>)” .. . 在 Parse 中,当我单击 User 类的回复列中的关系时,它会显示该用户喜欢的所有帖子。现在我不确定如何在代码中访问此列表(即:以便在检查哪些帖子被喜欢或不喜欢时,我可以在 if/else 控制块中使用它)。

标签: ios swift parse-platform social-media-like


【解决方案1】:

假设你有自定义 UITableViewCell 类并从 Parse 中获取数据,而不是你的 UITableView 数据源方法

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("skillsCell", forIndexPath: indexPath) as! YOURTableViewCell
    //Check If item is liked
    if (isLiked) {

      //Set image for like on button
      }
      else {
          //Set image for unlike on button
      }
    cell.YourButton.tag = indexPath.row // This will assign tag to each button in tableView

    return cell
}

比在 YourViewController 添加 UIButton Action

@IBAction func testButtonAction(sender: UIButton) {

    print(sender.tag)

    let cell = testTableView.cellForRowAtIndexPath(NSIndexPath(forRow: sender.tag, inSection: 0)) as! TestTableViewCell

    if cell.likeButton.titleLabel?.text == "Cell \(sender.tag)" { //Your logic here. Check If button's image has "Liked Image than change Image to UnLiked Image"
        cell.likeButton.text = "\(sender.tag)"
    }
    else {
        cell.likeButton.text = "Cell \(sender.tag)"
    } 
}

这将为 UITableView 中的按钮实现 Like/Unlike。还要确保相应地更新 Parse 中的类。

【讨论】:

  • 谢谢萨奇布。我在 Parse 中创建了一个 Like 类。请参阅更新的问题。不过有几个问题:1.) if (isLiked) 中的 bool isLiked 在哪里?我无法以这种方式访问​​ Parse。 2.)如果您的意思是在另一个文件中,我不确定您何时说“比在 YourViewController 添加 UIButton 操作”。我的 IBAction likeButtonTapped 和 cellForRowAtIndexPath 方法在同一个文件/类中实现:class TimelineFeedViewController: PFQueryTableViewController。 3)cell.likeButton.text = "\(sender.tag)" 在 if 和 else 语句中给了我这个错误:“'UIButton' 类型的值没有成员'text'。
  • 3,)(续)我不想更改likeButton 的text,只是更改likeButton 的image。感谢您澄清对这 3 个问题的回答!
猜你喜欢
  • 1970-01-01
  • 2020-03-10
  • 1970-01-01
  • 1970-01-01
  • 2016-05-10
  • 2019-02-28
  • 1970-01-01
  • 2014-03-29
  • 1970-01-01
相关资源
最近更新 更多