【发布时间】:2017-10-19 23:30:06
【问题描述】:
我有一个表格视图,单元格中填充了来自 Firebase 的数据。在每个单元格中都有一个点赞按钮,当我喜欢特定单元格中的按钮时,它会捕获该单元格的 ID 并在 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
我的问题是当我喜欢特定单元格上的一个按钮时,每个单元格中的所有类似按钮都会变成红色。但我只希望我单击的单元格变为红色,当我离开应用程序并返回时,所有按钮都恢复为白色。无论用户是否退出应用程序,我都希望登录用户喜欢的任何按钮保持红色。
【问题讨论】:
-
这个 IBAction
LikeClicked在哪里?在您的 viewController 或 tableViewCell 类中? -
@Glenn 它位于视图控制器中
标签: ios swift uitableview firebase-realtime-database