我想根据帐户类型在用户单元格中显示某个图像,因此firebase包含一个帐户类型:person的帐户我想显示一个人帐户图像,如果它包含帐户类型:我想要的商业帐户显示企业帐户图片。
我试过这个,但它不起作用
类UserCell:UITableViewCell {
func setAccountTypeImage(){
let AccountimageView = UIImageView()
let userID = FIRAuth.auth()?.currentUser?.uid
let ref = FIRDatabase.database().reference()
ref.child("Users").child(userID!).observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.hasChild("Person's Account User"){
AccountimageView.image = UIImage(named: "icon-72x72")
}else{
if snapshot.hasChild("Business Account User"){
AccountimageView.image = UIImage(named: "B icon-72x72")
}
}
})
}
override func layoutSubviews() {
super.layoutSubviews()
让AccountTypeImage:UIImageView = { 让AccountimageView = UIImageView()
AccountimageView.image = UIImage(named: "Grape")
AccountimageView.translatesAutoresizingMaskIntoConstraints = false
// the code below makes image a circle
//the value of 24 comes from half the width/hieght anchor constant of 48
AccountimageView.layer.cornerRadius = 20
AccountimageView.layer.masksToBounds = true
AccountimageView.contentMode = .scaleAspectFill
return AccountimageView
}()
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: .subtitle, reuseIdentifier: reuseIdentifier)
addSubview(AccountTypeImage)
AccountTypeImage.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 4).isActive = true
AccountTypeImage.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
AccountTypeImage.widthAnchor.constraint(equalToConstant: 40).isActive = true
AccountTypeImage.heightAnchor.constraint(equalToConstant: 40).isActive = true
}