我想在我的iOS應用中使用Swift 2.0,Storyboard和Parse實現類似/不同功能,用戶可以像/不像其他用戶或自己創建的帖子一樣使用Swift 2.0,Storyboard和Parse - 就像Instagram,Facebook和其他社交應用。在Swift中使用Parse實現像和不同功能
我有一個按鈕在Storyboard中連接到名爲likeButton的IBOutlet和名爲likeButtonTapped的IBAction。
我相信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()
}
}
你有UITableViewCell中的按鈕? –
@SaqibOmer我在一個單獨的swift類文件中實現了像likeButton等IBOutlets,它的子類UITableViewCell – dnadri
遞增和遞減通常更好地在雲代碼中完成。將關係存儲在關係中可以正常工作。 – Wain