<分区>
到目前为止,一旦用户登录,我就能够尽可能地填充我的文本标签。因此,一旦他们这样做,主页就会显示“欢迎 johndoe”(johndoe 是他们注册的用户名)。但是,我还有一个汉堡包样式的 TableView ,其中包含他们的个人资料信息,其中包含他们的用户名、位置和他们的个人资料图像。我在使用相同的用户名(即 johndoe)填充 TableView 中的用户名时遇到问题。
我尝试使用一个全局变量来保存用户名字符串,但我一直得到默认值“No Name”
var username : String = "No Name"
func getUserName() {
let databaseRef = Database.database().reference()
guard let userID = Auth.auth().currentUser?.uid else { return }
databaseRef.child("users").child(userID).observeSingleEvent(of: .value) { (snapshot) in
let theUserName = (snapshot.value as! NSDictionary)["nameOfUser"] as! String
self.username = theUserName
self.nameOfUserLabel.text! = "Welcome \(self.username)"
//this prints out the correct label by saying "Welcome johndoe"
}
print("The name of the user is: \(self.username)")
//for example it would print out in the console: "No Name"
}
通过使用用户名值,我试图将其分配给我的个人资料项目 TableView :
func createProfileArray() -> [ProfileItems] {
var tempProfileItems: [ProfileItems] = []
let profileItem = ProfileItems(profileImage: UIImage(named: "defaultUser")!, nameTitle: username, location: "Toronto")
tempProfileItems.append(profileItem)
return tempProfileItems
}
但是,一旦加载了 tableview,它只会显示“No name”。目标是使用与欢迎标题标签上相同的用户名。
感谢任何帮助。谢谢!