我正在尝试使用 Swift 2.0、Storyboard 和 Parse 在我的 iOS 应用程序中实现一个喜欢/不喜欢的功能,用户可以喜欢/不喜欢其他用户或他们自己创建的帖子 - 就像 Instagram、Facebook 和其他社交应用程序一样。
我在 Storyboard 中有一个连接到被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()
}
}