2

Instagram、Facebook、その他のソーシャルアプリと同様に、Swift 2.0、Storyboard、および Parse を使用して、iOS アプリにいいね/いいね! 機能を実装しようとしています。

IBOutletストーリーボードで、呼び出されlikeButtonたとIBAction呼び出された に接続されたボタンがありますlikeButtonTapped

cellForRowAtIndexPathメソッドもこの機能を正しく実装することに関与していると確信しています。

以下のコードのコメントで何をする必要があるかについては正しい考えがあると思いますが、特定の投稿が好きかどうかを確認する方法がわかりません。投稿が好きかどうかを確認して、画像を切り替えたり、 を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()
    }
}
4

1 に答える 1