【发布时间】:2021-11-29 14:29:21
【问题描述】:
我有两个用户默认值username 和password,我想设置tableView cell 内的标签文本这些用户默认值,但我的代码只设置username 而不是密码
有什么办法吗?
class UDM {
static let shared = UDM()
let defaults = UserDefaults(suiteName: "saved.data")
}
@IBAction func registerButton(_ sender: Any) {
UDM.shared.defaults?.setValue(usernameRegister.text, forKey: "username")
UDM.shared.defaults?.setValue(passwordRegister.text, forKey: "password")
} // userdefaults are set succesfully
// table view controller
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath) as? TableViewCell
cell?.setup(with: UDM.shared,with: UDM.shared)
return cell!
}
///table view cell
func setup(with username:UDM,with password:UDM){
label.text = UDM.shared.defaults?.value(forKey: "password") as? String
label.text = UDM.shared.defaults?.value(forKey: "username") as? String
}
【问题讨论】:
-
解决这个问题的原因有很多。 passwordRegister.text 是否包含一些值?最好用一些虚拟值测试表格视图标签。似乎您可能对用户名和密码使用相同的标签。也可以检查一下。 label.text = UDM.shared.defaults?.value(forKey: "password") as? String label.text = UDM.shared.defaults?.value(forKey: "username") as?字符串
标签: ios swift uitableview uikit