[英]How to change button's image when tapped using Firebase Realtime Database
I'm beginner at Swift.我是 Swift 的初学者。 I want to change the button's image after retrieving it from Firebase Realtime Database and when I tap.我想在从 Firebase 实时数据库中检索按钮图像并点击时更改按钮的图像。 I'm using a custom class named CustomClass
and I have tried to set the image if the boolean value in database is true and set it to nil if the value is false.我正在使用名为CustomClass
的自定义 class 并且如果数据库中的 boolean 值为 true,则我尝试设置图像,如果值为 false,则将其设置为 nil。
My issues:我的问题:
In cellForRowAt method,在 cellForRowAt 方法中,
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = InfoTableView.dequeueReusableCell(withIdentifier: "customcell", for: indexPath) as! CustomCell
let keyword = cell.nameLabel.text
var reference: DatabaseReference!
cell.buttonAction = { sender in
if (sender as AnyObject).imageView?.image != nil{
cell.prayButton.setImage(nil, for: .normal)
reference = Database.database().reference().child("check" ).child(keyword!).child("prayCheck")
reference.setValue(false)
} else{
let image = UIImage(named: "check")
cell.prayButton.setImage(image, for: .normal)
reference = Database.database().reference().child("check").child(keyword!).child("prayCheck")
reference.setValue(true)
}
}
cell.moneyButtonAction = { sender in
if (sender as AnyObject).imageView?.image != nil {
cell.moneyButton.setImage(nil, for: .normal)
reference = Database.database().reference().child("check").child(keyword!).child("moneyCheck")
reference.setValue(false)
} else {
let image = UIImage(named: "check")
cell.moneyButton.setImage(image, for: .normal)
reference = Database.database().reference().child("check").child(keyword!).child("moneyCheck")
reference.setValue(true)
}
}
cell.atentionButtonAction = { sender in
if (sender as AnyObject).imageView?.image != nil {
cell.alartButton.setImage(nil, for: .normal)
reference = Database.database().reference().child("check").child(keyword!).child("atentionCheck")
reference.setValue(false)
} else {
let image = UIImage(named: "check")
cell.alartButton.setImage(image, for: .normal)
reference = Database.database().reference().child("check").child(keyword!).child("atentionCheck")
reference.setValue(true)
}
}
return cell
}
In custom class,在定制 class,
class CustomCell: UITableViewCell {
@IBOutlet weak var bookingLabel: UILabel!
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var applyDateLabel: UILabel!
@IBOutlet weak var nameIcon: UILabel!
@IBOutlet weak var applyIcon: UILabel!
@IBOutlet weak var bookinfIcon: UILabel!
@IBOutlet weak var prayImage: UIImageView!
@IBOutlet weak var receiveMoneyImage: UIImageView!
@IBOutlet weak var atentionImage: UIImageView!
@IBOutlet weak var prayButton: UIButton!
@IBOutlet weak var moneyButton: UIButton!
@IBOutlet weak var alartButton: UIButton!
var pathKey: String!
var reference: DatabaseReference!
var buttonAction: ((Any) -> Void)?
var moneyButtonAction: ((Any) -> Void)?
var atentionButtonAction:((Any) -> Void)?
@IBAction func buttonPressed(_ sender: Any){
self.buttonAction?(sender)
}
@IBAction func moneyButtonPressed(_ sender: Any) {
self.moneyButtonAction?(sender)
}
@IBAction func atentionButtonPressed(_ sender: Any) {
self.atentionButtonAction?(sender)
}
}
Thank you.谢谢你。
It can be that reference - lost可能是那个参考 - 丢失
Try to make some refactoring尝试进行一些重构
cell.buttonAction = { sender in
configureAction(type: .moneyCheck)
}
And replace this code into function with 1 Parameter并用 1 个参数将此代码替换为 function
moneyCheck / atentionCheck / prayCheck钱支票/注意支票/祈祷支票
func configureAction(type: FieldTypeEnum) {
let isNotNull = (sender as AnyObject).imageView?.image != nil
reference.setValue(isNotNull)
reference = Database.database().reference().child("check").child(keyword!).child("prayCheck")
let image: UIImage? = isNotNil ? UIImage(named: "check") : nil
cell.prayButton.setImage(image, for: .normal)
}
Better if you will put into Enum如果您将放入 Enum 会更好
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.