2017-10-19 41 views
0

我有一個表格視圖,單元格中填充了來自Firebase的數據。在每個單元格中都有一個類似按鈕,當我喜歡特定單元格中的按鈕時,它會捕獲該單元格的ID並在Firebase中創建一個節點,讓我知道該按鈕被點擊(喜歡)。點擊按鈕之前,它是白色的,點擊它後變成紅色。然後,如果再次點擊(未點擊),它變成白色。將索引路徑發送到Firebase(如按鈕)

@IBAction func LikeClicked(_ sender: UIButton) -> Void { 

     let LikedRef = FIRDatabase.database().reference().child("Likes").child((self.loggedInUser?.uid)!) 

     let indexPath = self.selectedIndex 
     let post = self.posts![(indexPath?.row)!] as! [String: AnyObject] 
     self.key = post["postID"] as? String 

     let cell = TableView.cellForRow(at: indexPath!) as! ProfileTableViewCell 


     if cell.Like.currentImage == #imageLiteral(resourceName: "icons8-Hearts Filled-50 (2)"){ 
      cell.Like.setImage(#imageLiteral(resourceName: "icons8-Heart-50"), for: .normal) 
      // cell.RedLike.isHidden = true 

      FIRDatabase.database().reference().child("Likes").child((self.loggedInUser?.uid)!).child(self.key!).removeValue(completionBlock: { (error, ref) in 
       if error != nil { 
        print("error \(error)") 
       }else{ 

       }}) 
     } else{ 

      LikedRef.observeSingleEvent(of: .value, with: { (snapshot:FIRDataSnapshot) in 


       if let postsDictionary = snapshot .value as? [String: AnyObject] { 


        var LikeStatus = postsDictionary[self.key!] as? String ?? "" 

        if self.key == LikeStatus 
        { 
         // cell.Like.isHidden = true 

         cell.Like.setImage(#imageLiteral(resourceName: "icons8-Hearts Filled-50 (2)"), for: .normal) 

        } 


       }}) 



      LikedRef.updateChildValues([self.key!: self.key!]) 


     } 


    } 

    cell.Like.addTarget(self, action: #selector(LikeClicked), for: UIControlEvents.touchUpInside) 
    cell.Like.tag = indexPath.row 
    print(indexPath.row) 
    cell.Like.isUserInteractionEnabled = true 

我的問題是當我喜歡特定單元格上的一個按鈕時,每個單元格中的所有按鈕都變成紅色。但我只想要點擊的單元格變成紅色,當我離開應用程序並返回時,所有按鈕都變回白色。無論用戶是否退出應用程序,我都希望登錄用戶喜歡的任何按鈕保持紅色。

+0

這哪裏是IBAction爲''LikeClicked位於?在你的viewController或在你的tableViewCell類? – Glenn

+0

@Glenn它位於視圖控制器 – juelizabeth

回答

2

好吧,我花了一個小時左右的時間來給你一個想法,你如何做你需要做的事情。喜歡和不喜歡你的自定義UITableViewCell。我已經在每行代碼中解釋了我所做的細節。我希望這能夠幫到你。如果您有任何問題,請告訴我。記住,這只是你完成任務的很多方法之一。

MyViewController.swift

class MyViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, MyCustomCellDelegate { 

    // This is the array of keys that we 
    var likedDataKeys = [String]() 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Load here the 'Likes' stuff and store its in a datasource for reference or store as well its keys. 
     // If data is liked, store to likedDayaKeys the key of your data. 

     FirebaseCall { 
      if liked { 
       self.likedDataKeys.append(keyOfYourData) 
      } 
     } 
    } 

    // MARK: - UITableViewDataSource 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = .... 

     // Set the image. 

     let dataKey = yourDatasource[indexPath.row] // get the key or whatever data you need 

     // Set the delegate and key 
     cell.delegate = self 
     cell.dataKey = dataKey 

     if likedDataKeys.contains(dataKey) { 
      cell.image = redImageLike 
     } else { 
      cell.image = whiteNormalLikeImage 
     } 

     return cell 
    } 

    // MARK: - MyCustomCellDelegate 

    func myCustomCell(userDidTapLikeWithDataKey dataKey: String) { 
     // So now we can get the dataKey of the cell that is being liked or unliked. 
     // Check from the self.likedDataKeys if the tapped cell is liked or not. 

     if self.likedDataKeys.contains(dataKey) { 
      // If it is there, then we should call the unlike Firebase. 
      // Also remove it from the self.likedIndexPath and reload the tableView to update the image. 

      let index = self.likedDataKeys.index(of: dataKey) 
      self.likedDataKeys.remove(at: index) 

      // Call now the unlike Firebase. 

     } else { 
      // If it is not there, then we should call the like Firebase. 
      // Also store it to the self.likedIndexPAth 
     } 
    } 
} 

MyCustomCell.swift

protocol MyCustomCellDelegate: NSObjectProtocol { 
    // This is the delegate that will help us send the dataKey reference to the viewController 
    // Whenever the user taps on the like button in the cell. 
    func myCustomCell(userDidTapLikeWithDataKey dataKey: String) 
} 

class MyCustomCell: UITableViewCell { 

    // This will be called in the viewController, pass here the self of the viewController 
    weak var delegate: MyCustomCellDelegate? 

    // Make sure to pass here the key from the cellForRow of the viewController's tableView delegate. 
    var dataKey = "" 

    @IBAction func LikeClicked(_ sender: UIButton) -> Void { 
     // Call the delegate to inform the viewController 
     self.delegate?.myCustomCell(userDidTapLikeWithDataKey: self.dataKey) 
    } 
} 
+0

這非常有幫助!非常感謝你! – juelizabeth