現段階で色々試して、テーブルのデータを、ファイルを作成して
そこに保存することはできるようになりました。
しかし、1行目のデータだけや、指定した行目のデータしかファイルに保存さてていなく
実質、1行しかファイルに保存できていません。
今回教えていただきたいのは、テーブルの全行のデータをファイルに出力する方法です。
for文でループ処理も試みましたが、最後の行だけが出力され、またもや1行だけしか保存されませんでした。
処理として[SaveButtom]をタップしデータを作成したファイル(.csv)に保存します。
よろしくお願いします。
開発環境:Xcode8 Swift3
@IBAction func SaveButtom(_ sender: Any) { let name = self.name.text ?? "unknown" let thePath = NSHomeDirectory()+"/Documents/(name).csv" let count = toDoItems?.count ?? 0; for i in 0..<count { let textData = toDoItems?[i].name.description do { try textData?.write(toFile: thePath, atomically: true, encoding: String.Encoding.shiftJIS) } catch let error as NSError { print("保存に失敗。\n (error)") } } // let textData = toDoItems?[0].name.description // // do { // try textData?.write(toFile: thePath, atomically: true, encoding: String.Encoding.shiftJIS) // } catch let error as NSError { // print("保存に失敗。\n (error)") // } // ① UIAlertControllerクラスのインスタンスを生成 // タイトル, メッセージ, Alertのスタイルを指定する // 第3引数のpreferredStyleでアラートの表示スタイルを指定する let alert: UIAlertController = UIAlertController(title: "作成スコアを保存", message: "保存したデータは”データを送信する”からPCへ送信できます。", preferredStyle: UIAlertControllerStyle.alert) // ② Actionの設定 // Action初期化時にタイトル, スタイル, 押された時に実行されるハンドラを指定する // 第3引数のUIAlertActionStyleでボタンのスタイルを指定する // OKボタン let defaultAction: UIAlertAction = UIAlertAction(title: "保存", style: UIAlertActionStyle.default, handler:{ // ボタンが押された時の処理を書く(クロージャ実装) (action: UIAlertAction!) -> Void in print("保存") }) // キャンセルボタン let cancelAction: UIAlertAction = UIAlertAction(title: "保存しない", style: UIAlertActionStyle.cancel, handler:{ // ボタンが押された時の処理を書く(クロージャ実装) (action: UIAlertAction!) -> Void in print("保存しない") }) // ③ UIAlertControllerにActionを追加 alert.addAction(cancelAction) alert.addAction(defaultAction) // ④ Alertを表示 present(alert, animated: true, completion: nil) } @IBAction func addToDo(_ sender: Any) { if isValidateInputContents() == false{ return } // ToDoデータを作成する処理 let toDo = ToDo() toDo.name = todoNameText.text! // ToDoデータを永続化する処理 do{ let realm = try Realm() try realm.write{ realm.add(toDo) } todoNameText.text = "" }catch{ print("失敗") } tableView.reloadData() } private func isValidateInputContents() -> Bool{ // ToDo名のデータ入力 if let name = todoNameText.text{ if name.characters.count == 0{ return false } }else{ return false } return true } } extension ViewController: UITableViewDataSource{ func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return toDoItems?.count ?? 0 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let toDo = toDoItems?[indexPath.row] let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! ToDoTableViewCell // Realmに登録したデータをラベルに値設定 cell.nameLabel.text = toDo?.name print(toDo?.name) return cell } // 削除処理 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { if editingStyle == UITableViewCellEditingStyle.delete { let realm = try!Realm() _ = try!Realm().objects(ToDo.self) // これはRealmSwiftでデータを削除しているケース let deleteHistory = self.toDoItems?[indexPath.row] // トランザクションを開始してオブジェクトを削除します try! realm.write { realm.delete(deleteHistory!) } // TableViewを再読み込み. self.tableView.reloadData() } }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/01/05 10:41
2018/01/06 02:40
2018/01/06 18:06