###前提・実現したいこと
初心者です。現在、swiftでiOSの単純なメモアプリを作成しております。
テキストビューに書かれたテキストを移動先のシーンに受け渡し、
テーブルビューセルとして、保存し、削除、編集ができるようにするという内容にしたいと考えています。
###発生している問題・エラーメッセージ
下記の2点がわからず困っています。お手数ですが、原因がお分かりになる方がおりましたら、ご回答いただけますと幸いです。
① テキストをappAppDelegateを通して受け渡し、NSUserDefaultsを使い、保存、削除をすることができたのですが、作成済みのセルテキストの編集ができず困っています。(移動前の書き込みのページに保存されているテキストを受け渡し、編集後上書きしたいのですが、新たなセルとして保存されてしまいます。)
② わかりづらい説明になり申し訳ありませんが、下記の手順の操作をすると保存されているセルが全て消えてしまい、困っています。
・2つ以上のセルを作成する。
・編集ボタンテーブルビューページの編集ボタンメニューにて一つのセルを削除する。
・その後、画面遷移をし、再度テーブルビューページのを開くとセルが全て消えてしまいます。
エラーメッセージがでたり、止まってしまったりすることはないので、単純に処理をするタイミングがおかしいのかなと考えておりますが、知識が足りず、わかりません。
テーブルビュー側 swift import UIKit private let unselectedRow = -1 class memo_list: UIViewController,UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate { @IBOutlet weak var memoListView: UITableView! var memo1:String? var memo2:String? var memoList: [String] = [] var memoList2: [String] = [] var editRow: Int = unselectedRow override func viewDidLoad() { super.viewDidLoad() print(memo1) //保存したテキストデータをロード let hozon = NSUserDefaults.standardUserDefaults() let load = hozon.objectForKey("dly") if (load as? [String] != nil) { memoList = load as! [String] } let hozon3 = NSUserDefaults.standardUserDefaults() let load3 = hozon3.objectForKey("dly3") if (load3 as? [String] != nil) { memoList2 = load3 as! [String] } //配列の個数が合わない場合はリセットする if memoList.count != memoList2.count { memoList = [] memoList2 = [] NSLog("memoListとmemoList2に不整合が生じたので両方リセットしました") } //テキストを受け取る //セルにテキストを入れ込む処理 applyMemo() //編集ボタンを用意 navigationItem.rightBarButtonItem = editButtonItem() } override func setEditing(editing: Bool, animated: Bool) { super.setEditing(editing, animated: animated) memoListView.editing = editing } func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { memoListView.allowsSelectionDuringEditing = true return true } //セルを削除する処理 func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { if editingStyle == UITableViewCellEditingStyle.Delete { //指定したセルの番号の配列番号を削除する memoList.removeAtIndex(indexPath.row) memoList2.removeAtIndex(indexPath.row) tableView.deleteRowsAtIndexPaths([NSIndexPath(forRow: indexPath.row, inSection: 0)], withRowAnimation: UITableViewRowAnimation.Automatic) //NSUserDefaultsの方のデータも削除(ここでは配列の中の一つを削除しており、下のテキストで再度配列全体を保存し直す) let hozon = NSUserDefaults.standardUserDefaults() hozon.setObject(memoList, forKey: "dly") let hozon3 = NSUserDefaults.standardUserDefaults() hozon3.setObject(memoList2, forKey: "dly3") } } //セルにテキストを入れ込む処理 //新規か編集かを判断 func applyMemo() { if memo1 == nil { return } if memo1 == "" { return } if editRow == unselectedRow { memoList.append(memo1!) memoList2.append(memo2!) } else { print(memo1) memoList[editRow] = memo1! memoList2[editRow] = memo2! } //セル情報を保存する let hozon = NSUserDefaults.standardUserDefaults() hozon.setObject(memoList, forKey: "dly") let hozon3 = NSUserDefaults.standardUserDefaults() hozon3.setObject(memoList2, forKey: "dly3") //セルの選択状態を非選択に戻る editRow = unselectedRow //リロード処理(必須) memoListView.reloadData() } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return memoList.count } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let Cell = "Cell" let cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: Cell) if indexPath.row >= memoList.count { return cell } //タイトルとサブタイトルに配列からテキストを入れ込む cell.textLabel?.text = memoList[indexPath.row] cell.detailTextLabel?.text = memoList2[indexPath.row] return cell } //作成済みのセルを選択した時の処理 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { if indexPath.row >= memoList.count { return } editRow = indexPath.row memo1 = memoList[editRow] memo2 = memoList2[editRow] print(memo1) //書き込みページに画面遷移する // SubViewController へ遷移するために Segue を呼び出す performSegueWithIdentifier("settext1",sender: nil) } @IBAction func memo_tuika(sender: AnyObject) { memo1 = "" memo2 = "" performSegueWithIdentifier("settext1",sender: nil) } override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { let send2:SecondViewController = segue.destinationViewController as! SecondViewController if segue.identifier == "settext1" { if memo1 == nil { return } send2.title2 = memo1! send2.honbun2 = memo2! print(memo1) } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } テキスト入力画面 swift import UIKit class SecondViewController: UIViewController,UITableViewDelegate,UITextFieldDelegate { @IBOutlet weak var title_text: UITextField! @IBOutlet weak var honbun_text: textview_custom! var title2:String? var honbun2:String? override func viewDidLoad() { super.viewDidLoad() title_text.delegate = self } //変更点:前回はセル選択時のテキストの受け取りをviewDidLoad()で行ってしまっており、navigationController?.popViewControllerAnimated(true)で戻ってきた場合、処理が行われていなかったため、毎回の遷移全てで反応するviewWillAppearにて行うよう、変更しました。 override func viewWillAppear(animated: Bool) { title_text.text = self.title2 honbun_text.text = self.honbun2 } //移動の際に向こうのプロバティにテキストを引き継ぐ? override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if segue.identifier == "sendtext" { let send:memo_list = segue.destinationViewController as! memo_list send.memo1 = title_text.text send.memo2 = honbun_text.text //その後、初期化する title_text.text = "" honbun_text.text = "" } } //キーボードを下げる処理 override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { if(title_text.isFirstResponder()){ title_text.resignFirstResponder() } if(honbun_text.isFirstResponder()){ honbun_text.resignFirstResponder() } } //保存ボタンアクション @IBAction func save(sender: AnyObject) { performSegueWithIdentifier("sendtext",sender: nil) } //保存(セルの追加)処理をしないで、テーブルビューページに移動する処理 @IBAction func listbotton(sender: AnyObject) { title_text.text = "" honbun_text.text = "" performSegueWithIdentifier("sendtext",sender: nil) } //仮で作った戻るボタン //prepareForSegueが反応してくれない @IBAction func back(sender: UIStoryboardSegue) { let x = self.storyboard?.instantiateViewControllerWithIdentifier("memo_list") x?.modalTransitionStyle = .CoverVertical navigationController?.popViewControllerAnimated(true) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } } AppDelegate swift import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? // メモ用定数 var title:String? var honbun:String? func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { return true } func applicationWillResignActive(application: UIApplication) { } func applicationDidEnterBackground(application: UIApplication) { } func applicationWillEnterForeground(application: UIApplication) { } func applicationDidBecomeActive(application: UIApplication) { } func applicationWillTerminate(application: UIApplication) { } }
###試したこと
初心者で知識が少ないため、テキストを受け渡すタイミングを変えるため、コードを書く位置を変えてみるということしかできず、問題解決ができない状態です。
###補足情報(言語/FW/ツール等のバージョンなど)
xcode Version 7.3.1 (7D1014)
swift2
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/06/13 04:03
2016/06/13 17:55
2016/06/15 17:59 編集
2016/06/15 14:59
2016/06/15 18:46
2016/06/15 19:08
2016/06/16 14:39
2016/06/16 23:52
2016/06/18 19:14
2016/06/18 23:58 編集
2016/06/20 22:35