我有一个表格视图,单元格中填充了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
我的问题是,当我喜欢特定单元格上的一个按钮时,每个单元格中的所有类似按钮都会变为红色。但我只想让我点击的单元格变成红色,当我离开应用程序并返回时,所有按钮都恢复为白色。我希望登录用户喜欢的任何按钮保持红色,无论用户是否退出应用程序。
答案 0 :(得分: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)
}
}