所以我有一个UITable,每当我更改其中的内容,然后点击保存,数据就会被发送到我的DJANGO数据库并正确保存所有信息。 但是,当我离开视图然后返回到它时,表不会从数据库中获取当前数据并显示我保存之前的数据。再次,数据在我的数据库中正确保存,但我想在本地存储数据的数组不会更新?我正在使用导航视图,我正在执行segue来显示表格。 这是我的代码:
import UIKit
class LifestyleTableViewController: UITableViewController {
// MARK: - View lifecycle
var isOns: [Int] = []
var chunks: [[Int]] = [[],[]]
let emojiUnicodesMale: Array = [["", "⚽⚾", "️", "", "", "\u{1F483}", "\u{1F3AE}"], ["\u{1F4AA}", "\u{1F3C0}", "\u{1F3BE}", "\u{26BD}", "\u{26BE}", "\u{1F3B5}", "\u{1F3B1}"], ["\u{1F3A8}", "\u{1F3A4}", "\u{1F3A6}", "\u{1F3A3}", "\u{1F37B}", "\u{1F355}", "\u{1F3B3}"]]
let emojiUnicodesFemale: Array = [["", "⚽⚾", "️", "", "", "\u{1F483}", "\u{1F3AE}"], ["\u{1F4AA}", "\u{1F3C0}", "\u{1F3BE}", "\u{26BD}", "\u{26BE}", "\u{1F3B5}", "\u{1F3B1}"],["\u{1F3A8}", "\u{1F3A4}", "\u{1F3A6}", "\u{1F3A3}", "\u{1F37B}", "\u{1F355}", "\u{1F3B3}"]]
let emojiDescriptions: Array = [["Clothing Shopping", "Sport", "Tech", "Grocery", "Reading", "Dancing", "Games"], ["Gym Workout", "Basketball", "Tennis", "Football", "Baseball", "Music", "Snorker"], ["Painting", "Singing", "Shooting", "Fishing", "Drinking", "Pizza Lover", "Bowling"]]
override func viewDidLoad() {
super.viewDidLoad()
//self.tableView = UITableView(frame: self.tableView.frame, style: .grouped)
self.tableView.tableFooterView = UIView()
getLifestyle()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func viewWillAppear(_ animated: Bool) {
getLifestyle()
}
override func viewDidAppear(_ animated: Bool) {
getLifestyle()
}
// MARK: - Table view data source
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 60.0
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 3
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 7
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if(section == 1) {
return "Food"
} else {
return "Shopping"
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "EmojiCell", for: indexPath) as! EmojiTableViewCell
if (User.currentUser.gender == "female") {
cell.labelEmojiUnicode.text = emojiUnicodesFemale[indexPath.section][indexPath.row]
} else {
cell.labelEmojiUnicode.text = emojiUnicodesMale[indexPath.section][indexPath.row]
}
cell.labelDescription.text = emojiDescriptions[indexPath.section][indexPath.row]
if isOns.count > 0 {
if indexPath.section == 0 {
switch(indexPath.row) {
case 0:
cell.buttonSwitch.isOn = isOns[0] == 0 ? false : true
cell.buttonSwitch.restorationIdentifier = "0"
case 1:
cell.buttonSwitch.isOn = isOns[1] == 0 ? false : true
cell.buttonSwitch.restorationIdentifier = "1"
case 2:
cell.buttonSwitch.isOn = isOns[2] == 0 ? false : true
cell.buttonSwitch.restorationIdentifier = "2"
case 3:
cell.buttonSwitch.isOn = isOns[3] == 0 ? false : true
cell.buttonSwitch.restorationIdentifier = "3"
case 4:
cell.buttonSwitch.isOn = isOns[4] == 0 ? false : true
cell.buttonSwitch.restorationIdentifier = "4"
case 5:
cell.buttonSwitch.isOn = isOns[5] == 0 ? false : true
cell.buttonSwitch.restorationIdentifier = "5"
case 6:
cell.buttonSwitch.isOn = isOns[6] == 0 ? false : true
cell.buttonSwitch.restorationIdentifier = "6"
default:
cell.buttonSwitch.restorationIdentifier = "\(indexPath.row)"
}
}
if indexPath.section == 1 {
switch(indexPath.row) {
case 0:
cell.buttonSwitch.isOn = isOns[7] == 0 ? false : true
cell.buttonSwitch.restorationIdentifier = "7"
case 1:
cell.buttonSwitch.isOn = isOns[8] == 0 ? false : true
cell.buttonSwitch.restorationIdentifier = "8"
case 2:
cell.buttonSwitch.isOn = isOns[9] == 0 ? false : true
cell.buttonSwitch.restorationIdentifier = "9"
case 3:
cell.buttonSwitch.isOn = isOns[10] == 0 ? false : true
cell.buttonSwitch.restorationIdentifier = "10"
case 4:
cell.buttonSwitch.isOn = isOns[11] == 0 ? false : true
cell.buttonSwitch.restorationIdentifier = "11"
case 5:
cell.buttonSwitch.isOn = isOns[12] == 0 ? false : true
cell.buttonSwitch.restorationIdentifier = "12"
case 6:
cell.buttonSwitch.isOn = isOns[13] == 0 ? false : true
cell.buttonSwitch.restorationIdentifier = "13"
default:
cell.buttonSwitch.restorationIdentifier = "\(indexPath.row)"
}
}
if indexPath.section == 2 {
switch(indexPath.row) {
case 0:
cell.buttonSwitch.isOn = isOns[14] == 0 ? false : true
cell.buttonSwitch.restorationIdentifier = "14"
case 1:
cell.buttonSwitch.isOn = isOns[15] == 0 ? false : true
cell.buttonSwitch.restorationIdentifier = "15"
case 2:
cell.buttonSwitch.isOn = isOns[16] == 0 ? false : true
cell.buttonSwitch.restorationIdentifier = "16"
case 3:
cell.buttonSwitch.isOn = isOns[17] == 0 ? false : true
cell.buttonSwitch.restorationIdentifier = "17"
case 4:
cell.buttonSwitch.isOn = isOns[18] == 0 ? false : true
cell.buttonSwitch.restorationIdentifier = "18"
case 5:
cell.buttonSwitch.isOn = isOns[19] == 0 ? false : true
cell.buttonSwitch.restorationIdentifier = "19"
case 6:
cell.buttonSwitch.isOn = isOns[20] == 0 ? false : true
cell.buttonSwitch.restorationIdentifier = "20"
default:
cell.buttonSwitch.restorationIdentifier = "\(indexPath.row)"
}
}
cell.buttonSwitch.addTarget(self, action: #selector(LifestyleTableViewController.switchChanged), for: .valueChanged)
}
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
// MARK: - Actions
func getLifestyle() {
isOns.removeAll()
let lifestyle = User.currentUser.lifestyle
let array = lifestyle?.components(separatedBy: ",")
if let ary = array {
for item in ary {
isOns.append(Int(item)!)
}
}
print(isOns)
self.tableView.reloadData()
}
func switchChanged(selectedSwitch: UISwitch) {
if selectedSwitch.isOn {
self.isOns[Int(selectedSwitch.restorationIdentifier!)!] = 1
} else {
self.isOns[Int(selectedSwitch.restorationIdentifier!)!] = 0
}
}
@IBAction func save(_ sender: Any) {
var lifestyle: String = ""
for (index, item) in self.isOns.enumerated() {
lifestyle += String(item)
if index != self.isOns.count-1 {
lifestyle += ","
}
}
let params: [String: Any] = [
"race": "",
"phone": "",
"lifestyle_info": lifestyle,
"gender_pref": "",
"race_pref": "",
"budget_pref": "",
"move_in_pref": ""
]
APIManager.shared.updateUserProfile(params: params, completionHandler: { json in
if json != nil {
let alert = UIAlertController(title: "Successfully Saved!", message: nil, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
} else {
let alert = UIAlertController(title: "There is error saving your lifestyle info!", message: nil, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Error", style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
}
})
}
}
我的getLifeStyle()可能有问题吗?或者可能没有重新加载表,并且数组没有更新。 我迷路了,我已经尝试了一切。我真的不知道它会是什么。