###前提・実現したいこと
swiftで課題リストを作ってます
###発生している問題・エラーメッセージ
fatal error: Index out of range //100行目 エラーの内容はわかるのですがなぜなっているのかがわかりません どうしたらいいのでしょうか?
###該当のソースコード
swift
1import UIKit 2 3class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 4 5 @IBOutlet var tableView: UITableView! 6 var todoarray = [String]() 7 var todoarraydate = [String]() 8 var todocontent = [String]() 9 var refresher: UIRefreshControl? 10 11 override func viewDidLoad() { 12 super.viewDidLoad() 13 if UserDefaults.standard.array(forKey: "todoarray") == nil { 14 todoarray = [String]() 15 UserDefaults.standard.set(todoarray, forKey: "todoarray") 16 } else { 17 todoarray = UserDefaults.standard.array(forKey: "todoarray") as! [String] 18 } 19 20 if UserDefaults.standard.array(forKey: "todoarraydate") == nil { 21 todoarraydate = [String]() 22 UserDefaults.standard.set(todoarraydate, forKey: "todoarraydate") 23 } else { 24 todoarraydate = UserDefaults.standard.array(forKey: "todoarraydate") as! [String] 25 } 26 27 if UserDefaults.standard.array(forKey: "todocontent") == nil { 28 todocontent = [String]() 29 UserDefaults.standard.set(todocontent, forKey: "todocontent") 30 } else { 31 todocontent = UserDefaults.standard.array(forKey: "todocontent") as! [String] 32 } 33 34 refresher = UIRefreshControl() 35 36 refresher?.attributedTitle = NSAttributedString(string: "Pull to refresh") 37 38 refresher?.addTarget(self, action: #selector(FirstViewController.refresh), for: UIControlEvents.valueChanged) 39 40 self.tableView.addSubview(refresher!) 41 42 refresh() 43 // Do any additional setup after loading the view, typically from a nib. 44 } 45 46 override func didReceiveMemoryWarning() { 47 super.didReceiveMemoryWarning() 48 // Dispose of any resources that can be recreated. 49 } 50 51 internal func numberOfSections(in tableView: UITableView) -> Int { 52 return 1 53 } 54 55 func refresh() { 56 tableView.reloadData() 57 refresher?.endRefreshing() 58 } 59 60 internal func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 61 if todoarray.count == todoarraydate.count { 62 return todoarray.count 63 } else { 64 return todoarraydate.count 65 } 66 } 67 68 internal func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 69 var cell = tableView.dequeueReusableCell(withIdentifier: "cell") 70 if cell == nil { 71 cell = UITableViewCell(style: .default, reuseIdentifier: "cell") 72 } 73 cell?.textLabel?.text = todoarray[indexPath.row] 74 cell?.detailTextLabel?.text = String(describing: todoarraydate[indexPath.row]) 75 return cell! 76 } 77 78 internal func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { 79 // Return false if you do not want the specified item to be editable. 80 return true 81 } 82 83 @IBAction func new(_ sender: AnyObject) { 84 self.performSegue(withIdentifier: "New To Do", sender: self) 85 } 86 87 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 88 if editingStyle == .delete { 89 todoarray.remove(at: indexPath.row) 90 todoarraydate.remove(at: indexPath.row) 91 print("a") 92100 todocontent.remove(at: indexPath.row) 93 print("b") 94 tableView.deleteRows(at: [indexPath], with: .fade) 95 UserDefaults.standard.set(todoarray, forKey: "todoarray") 96 UserDefaults.standard.set(todoarraydate, forKey: "todoarraydate") 97 UserDefaults.standard.set(todocontent, forKey: "todocontent") 98 } 99 } 100 101 //画面遷移 102 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 103 104 performSegue(withIdentifier: "toSubViewController", sender: nil) 105 } 106 107} 108 109
###試したこと
分からないことがあれば質問してください よろしくお願いします
回答1件
あなたの回答
tips
プレビュー