2017-02-01 21 views
0

セルがタップされたときに編集できるセル[1, 2, 3, 4, 5]の5つの配列を持つuitableviewのアプリケーションを作成しました。私はあなたがセルをタップすると、それはalertviewとテキストフィールドを示しています。セルを短く編集することができます。私は別のビューコントローラに行くとき、私は編集を保存しています。しかし、問題は、私がアプリケーションを閉じてそれを開くしようとすると、それは[1,2,3,4,5]のようなデフォルトの数のメッセージに戻ります。どのようにヒントを編集したセルを保存することができますが、同時にuitableviewが開いているときに、配列を示しています。私の英語のために申し訳ありません。私はこのコードをプログラミング言語に申し訳ありません新しいですところでおかげアプリケーションが終了するとuserdefaultsを保存できません

* Messages.Swift =テーブルビュー

class Messages: UITableViewController { 

    @IBOutlet var uitableView: UITableView! 

    var tField: UITextField! 
    var index: Int? 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     tableView.delegate = self 
     tableView.dataSource = self 
    } 

    override func viewWillAppear(_ animated: Bool) { 

     //Get the array 
     if let array = UserDefaults.standard.object(forKey:"messages") as? Array<String> { 
      self.app.helper.messages = array 
     } 
    } 

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return app.helper.messages.count 
    } 

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

     let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) 
     let messagesCell = app.helper.messages[indexPath.row] 
     cell.textLabel?.text = messagesCell 

     return cell 

    } 

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 

     let path = tableView.indexPathForSelectedRow 
     index = path?.row 

     changeMessage() 
    } 

    func changeMessage() { 

     let alert = UIAlertController(title: "Message", message: "Enter new preset message", preferredStyle: UIAlertControllerStyle.alert) 

     alert.addTextField(configurationHandler: configurationTextField) 

     let doneAction = UIAlertAction(title: "Save", style: UIAlertActionStyle.default, handler:{ (UIAlertAction) in 

      let modelString = self.tField.text 
      self.app.helper.messages[self.index!] = modelString! 
      self.app.helper.saveToDefaults() 
      self.tableView.reloadData() 

     }) 

     let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) 

     alert.addAction(doneAction) 
     alert.addAction(cancelAction) 
     self.present(alert, animated: true, completion: nil) 
    } 

    func configurationTextField(textField: UITextField!){ 
     if (textField) != nil { 
      tField = textField 
      textField.text = app.helper.messages[index!] 
     } 
    } 
} 

* Helper.swift

class Helper { 

var messages: [String] = ["1", "2", "3", "4", "5"] 

    func saveToDefaults() { 
     let defaults = UserDefaults.standard 
     defaults.set(messages, forKey: "messages") 
     defaults.synchronize() 

    } 
} 

答えて

0

テーブルビューのデリゲートを設定する前に、ユーザーのデフォルトをロードしよう。テーブルビューはヘルパーで割り当てたデータセットでロードされているため、デフォルトのデータセットではロードされていません。

override func viewDidLoad() { 
    super.viewDidLoad() 

    //Get the array 
    if let array = UserDefaults.standard.object(forKey:"messages") as? Array<String> { 
     self.app.helper.messages = array 
    } 

    tableView.delegate = self 
    tableView.dataSource = self 
} 

それとも

override func viewWillAppear(_ animated: Bool) { 

    //Get the array 
    if let array = UserDefaults.standard.object(forKey:"messages") as? Array<String> { 
     self.app.helper.messages = array 
    } 
    tableView.reloadData() 
} 
+0

が、最初にあなたを試すユーザーdefautからデータセットをロードした後に表示されるビューでテーブルビューを再ロードすることができますデータがaであることを確認する必要がありますctuallyロードされます。 「self.app.helper.messages」にデータが入力されていることを確認しましたか? – Russell

+0

はいapp.helper.messagesは、入力されたテキストフィールドによって入力されています。 –

0

override func viewWillAppear(_ animated: Bool) { 

    //Get the array 
    if let array = UserDefaults.standard.object(forKey:"messages") as? Array<String> { 
     self.app.helper.messages = array 
    } 
    //make this array global(declare it below var index: Int?) 
    tableView.reloadData() 
} 

let messagesCell = array[indexPath.row] 
関連する問題