2017-10-19 50 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