前提・実現したいこと
Swift4でtableの追加,編集機能を実装しようとしています.
AddGroupViewControllerのtextFieldに値を入力してボタンを押下すると,ViewControllerのTableViewに値が追加されるというように実装したいと考えています.
発生している問題・エラーメッセージ
tableに表示するための配列に入力は成功していますが,reloadData()を行うとThread 1: signal SIGABRT
が発生します.
###StoryBoard
unwindには「GoToViewController」というIdentifierを設定しています.
該当のソースコード
・viewController
swift4
1import UIKit 2 3class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource { 4 5 var GroupTitle:[String] = [] 6 7 @IBOutlet weak var GroupTable: UITableView! 8 @IBOutlet weak var addButton: UIBarButtonItem! 9 10 //ここに戻ってくる時の処理 11 @IBAction func GoToViewController(_ segue : UIStoryboardSegue){ 12 } 13 14 override func viewDidLoad() { 15 super.viewDidLoad() 16 // Do any additional setup after loading the view, typically from a nib. 17 GroupTable.delegate = self 18 GroupTable.dataSource = self 19 20 //セルの編集buttonの追加 21 self.navigationController?.isNavigationBarHidden = false 22 navigationItem.leftBarButtonItem = editButtonItem 23 GroupTable.reloadData() 24 } 25 26 // Table Viewにいくつのセルを表示するかを指定する 27 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 28 return GroupTitle.count 29 } 30 31 // セルに表示する値を設定する 32 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 33 // データを表示するセルを取得する 34 let GroupCell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "Groupcell", for: indexPath) 35 36 // セルに表示する値を設定する 37 GroupCell.textLabel!.text = GroupTitle[indexPath.row] 38 39 // データを設定したセルを返却する 40 return GroupCell 41 } 42 //セルの編集 43 override func setEditing(_ editing: Bool, animated: Bool) { 44 //override前の処理を継続してさせる 45 super.setEditing(editing, animated: animated) 46 GroupTable.isEditing = editing 47 48 //addButton有効化,無効化 49 if editing { 50 addButton.isEnabled = false 51 } 52 else{ 53 addButton.isEnabled = true 54 } 55 } 56 func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { 57 return true 58 } 59 60 //編集ボタンが押下された時の処理 61 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) { 62 63 //dataを消してから 64 GroupTitle.remove(at: indexPath.row) 65 //tableViewCellの削除 66 tableView.deleteRows(at: [indexPath], with: .automatic) 67 GroupTable.reloadData() 68 } 69 70 override func didReceiveMemoryWarning() { 71 super.didReceiveMemoryWarning() 72 // Dispose of any resources that can be recreated. 73 } 74}
・AddGroupViewController
swift4
1import UIKit 2 3class AddGroupViewController: UIViewController { 4 5 @IBOutlet weak var nameTextField: UITextField? 6 var addName : String? 7 8 @IBAction func add(_ sender: Any) { 9 addName = nameTextField?.text 10 } 11 12 //ビュー遷移時にグループ追加処理 13 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 14 if segue.identifier == "GoToViewController"{ 15 let TopView:ViewController = segue.destination as! ViewController 16 17 //addNameが空じゃないときViewControllerのGroupTitleに値を追加 18 if let text = addName, !text.isEmpty { 19 TopView.GroupTitle.append(addName!); 20 21 //ここのrerloadData()をコメントアウトするとエラーはきえるが,Tableが更新されない 22 TopView.GroupTable.reloadData() 23 24 print(TopView.GroupTitle) //値が格納されているか確認 25 26 }else{ 27 print("not add text") 28 } 29 } 30 } 31 32 override func viewDidLoad() { 33 super.viewDidLoad() 34 // Do any additional setup after loading the view. 35 } 36 37 override func didReceiveMemoryWarning() { 38 super.didReceiveMemoryWarning() 39 // Dispose of any resources that can be recreated. 40 } 41}
試したこと
別viewでTableViewを更新することがいけないのかと思い,ViewControllerで同様な処理を書き直しましたが,同じくThread 1: signal SIGABRT
が発生しました.
補足情報
・Xcode 10.0
・swift 4.2
・iOS 12
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/10/07 05:52 編集
2018/10/07 06:04
2018/10/07 14:38
2018/10/07 16:47
2018/10/08 01:36
2018/10/08 01:43
2018/10/09 08:26
2018/10/09 08:30
2018/10/09 13:27