I have array NSUserDefaults
type :
我有阵列NSUserDefaults类型:
let userValue = ["name": self.nameLbl.text ?? "", "lastName": self.lastNameLbl.text ?? "", "city": cityLbl.text ?? ""] as! [String : String]
var userArray = UserDefaults.standard.array(forKey: "userInfo") as? [[String: String]] ?? []
UserDefaults.standard.set(userArray, forKey: "userInfo")
And save this array in first `ViewController`
In second ViewController
Im display this array in UITAbleView
在第二个ViewController中我在UITAbleView中显示这个数组
class ...
var userInfo = [[String:String]]()
override func viewDidLoad() {
super.viewDidLoad()
userInfo = UserDefaults.standard.array(forKey: "userInfo") as? [[String: String]] ?? []
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! UserTableViewCell
var userItem = userInfo[indexPath.row]
cell.nameLbl?.text = item["name"]
cell.lastNameLbl.text = item["lastName"]
cell.cityLbl.text = item["city"]
and under UITAbleView
I have 3 UIextField's
and UIButton
for the opportunity to change user information
在UITAbleView下我有3个UIextField和UIButton,可以更改用户信息
When user pass text in UIextField's
I want to resave value for key
当用户在UIextField中传递文本时,我想为key重新保存值
How can I remove selected name and save new name to correctly user??
如何删除所选名称并将新名称保存到正确的用户?
To Abhirajsinh Thakore answer :
致Abhirajsinh Thakore回答:
Im update code in cellForRowAt
我在cellForRowAt中更新代码
var userItem = userInfo[indexPath.row]
cell.nameLbl?.text = item["name"]
cell.lastNameLbl.text = item["lastName"]
cell.cityLbl.text = item["city"]
item["name"] = cell.nameLbl?.text
var maArray = UserDefaults.standard.array(forKey: "userInfo") as? [[String: String]] ?? []
item.updateValue(cell.nameLbl?.text, forKey: "name")
maArray.append(item)
But its not save
但它不能保存
1 个解决方案
#1
0
You can fetch your complete array from Userdefaults
您可以从Userdefaults获取完整的数组
Now do is that get the new values from textField
and replace that at the specific indexPath.row
现在做的是从textField获取新值并在特定的indexPath.row处替换它
For e.g:
var userInfo = [[String:String]]()
let data = userInfo[indexPath.row]
data["name"] = txtName.text
data["address"] = txtAddress.text
And now store this full array back to userDefaults
and you will get the updated Result every Time.
现在将此完整数组存储回userDefaults,您将每次都获得更新的结果。
Hope this helps.
希望这可以帮助。
Edit with reference to the code
Do like this:
这样做:
var maArray = UserDefaults.standard.array(forKey: "userInfo") as? [[String: String]] ?? []
let innerData = maArray[indexPath.row]
innerData["name"] = cell.nameLbl?.text
innerData["lastName"] = cell.lastNameLbl.text
innerData["city"] = cell.cityLbl.text
// And save the maArray back to userDefaults
#1
0
You can fetch your complete array from Userdefaults
您可以从Userdefaults获取完整的数组
Now do is that get the new values from textField
and replace that at the specific indexPath.row
现在做的是从textField获取新值并在特定的indexPath.row处替换它
For e.g:
var userInfo = [[String:String]]()
let data = userInfo[indexPath.row]
data["name"] = txtName.text
data["address"] = txtAddress.text
And now store this full array back to userDefaults
and you will get the updated Result every Time.
现在将此完整数组存储回userDefaults,您将每次都获得更新的结果。
Hope this helps.
希望这可以帮助。
Edit with reference to the code
Do like this:
这样做:
var maArray = UserDefaults.standard.array(forKey: "userInfo") as? [[String: String]] ?? []
let innerData = maArray[indexPath.row]
innerData["name"] = cell.nameLbl?.text
innerData["lastName"] = cell.lastNameLbl.text
innerData["city"] = cell.cityLbl.text
// And save the maArray back to userDefaults