2015-12-04 25 views
2

我想在我的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() 

} 

} 
+0

你有UITableViewCell中的按鈕? –

+0

@SaqibOmer我在一個單獨的swift類文件中實現了像likeButton等IBOutlets,它的子類UITableViewCell – dnadri

+0

遞增和遞減通常更好地在雲代碼中完成。將關係存儲在關係中可以正常工作。 – Wain

回答

-1

假設你有自定義的UITableViewCell類,並具有解析取得的數據,比你的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的行動

@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中的按鈕。另外請確保您相應地更新您的課程。

+0

謝謝Saqib。我在Parse中創建了一個Like類。查看更新的問題。很少有問題,但: 1)bool在'if(isLiked)'中被綁定到哪裏?我無法通過這種方式訪問​​Parse。 2.)我不確定當你說「比在YourViewController添加UIButton行動」,如果你的意思是在另一個文件。我的IBAction likeButtonTapped和'cellForRowAtIndexPath'方法在同一個文件/類中實現:'class TimelineFeedViewController:PFQueryTableViewController'。 3)'cell.likeButton.text =「\(sender.tag)」'在if和else語句中給了我這個錯誤:「類型'UIButton'的值沒有成員'文本' – dnadri

+0

3,)(繼續)我不想改變likeButton的_text_,只是likeButton的_image_,我很感謝你澄清你對這3個問題的回答! – dnadri

相關問題