いつもお世話になっております。
TableViewのCellを選択したら画面遷移し、スワイプすると削除ボタンを表示する機能の実装を行なっております。
スワイプすると削除ボタンを表示することはできましたが、そのボタンを押してもエラーが出てしまいます。
削除するコードをネットで検索し、それを引用してもエラーが出るのでteratailにて相談させていただく運びとなりました。
コードの添削、アドバイスなどをしていただけたら幸いです。
どうかよろしくお願いします。
<<テーブルビューのswift>>
class
1 2 @IBOutlet weak var tableView: UITableView! 3 4 var appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate 5 6 //保存した画像 7 var filePaths: String! 8 //表示枚数 9 var saveImage1: UIImage? 10 var saveImage2: UIImage? 11 12 13 //保存した日付 14 //var saveDates: String! 15 //表示個数 16 var saveDate1: String! 17 var saveDate2: String! 18 19 20 override func viewDidLoad() { 21 super.viewDidLoad() 22 23 tableView.delegate = self 24 tableView.dataSource = self 25 tableView.estimatedRowHeight = 100.0 26 tableView.rowHeight = tableView.bounds.height / 4.0 27 28 } 29 30 override func didReceiveMemoryWarning() { 31 super.didReceiveMemoryWarning() 32 // Dispose of any resources that can be recreated. 33 } 34 35 36 override func viewWillAppear(animated: Bool) { 37 super.viewWillAppear(animated) 38 print(appDelegate.filePathArray.count) 39 if appDelegate.filePathArray.count == 1 { 40 41 //画像 42 filePaths = appDelegate.filePathArray[0] 43 saveImage1 = UIImage(contentsOfFile: filePaths) 44 //日付 45 saveDate1 = appDelegate.dateFileArray[0] 46 47 } else if appDelegate.filePathArray.count > 1 { 48 49 for index in appDelegate.filePathArray.startIndex..<appDelegate.filePathArray.endIndex { 50 51 switch index { 52 case 0: 53 //画像 54 filePaths = appDelegate.filePathArray[0] 55 saveImage1 = UIImage(contentsOfFile: filePaths) 56 //日付 57 saveDate1 = appDelegate.dateFileArray[0] 58 case 1: 59 //画像 60 filePaths = appDelegate.filePathArray[1] 61 saveImage2 = UIImage(contentsOfFile: filePaths) 62 //日付 63 saveDate2 = appDelegate.dateFileArray[1] 64 65 default: break 66 } 67 68 } 69 } 70 71 } 72 73 74 75 76 // MARK: - TableView 77 func numberOfSectionsInTableView(tableView: UITableView) -> Int { 78 79 let count = appDelegate.filePathArray.count 80 return count 81 } 82 83 func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 84 return 20 // セルの上部のスペース 85 } 86 87 //セルの行数 88 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 89 return 1 90 } 91 92 //スワイプによるセルの削除を許可 93 func tableView(tableView: UITableView,canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { 94 return true 95 } 96 97 //セルの削除ボタンが押されたときの処理 98 func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 99 if editingStyle == UITableViewCellEditingStyle.Delete { 100 /* 101 appDelegate.filePathArray.removeAtIndex(indexPath.row) 102 appDelegate.dateFileArray.removeAtIndex(indexPath.row) 103 */ 104 //tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic) 105 tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic) 106 } 107 } 108 109 110 //セルの部品を追加 111 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 112 let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as UITableViewCell 113 cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator 114 115 let imageView = cell.viewWithTag(1) as! UIImageView 116 let label: UILabel = cell.viewWithTag(2) as! UILabel 117 118 switch indexPath.section { 119 case 0: 120 imageView.image = saveImage1 121 label.text = saveDate1 122 case 1: 123 imageView.image = saveImage2 124 label.text = saveDate2 125 126 default: break 127 } 128 129 return cell 130 } 131 132 //セルがタップされたとき 133 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 134 switch indexPath.section { 135 case 0: 136 let STV = storyboard?.instantiateViewControllerWithIdentifier("SelfTemplate") as! SelfTemplateViewController 137 STV.selfTemplate = saveImage1 138 appDelegate.selfTemplateSelection = true 139 self.navigationController!.pushViewController(STV, animated: true) 140 case 1: 141 let STV = storyboard?.instantiateViewControllerWithIdentifier("SelfTemplate") as! SelfTemplateViewController 142 STV.selfTemplate = saveImage2 143 appDelegate.selfTemplateSelection = true 144 self.navigationController!.pushViewController(STV, animated: true) 145 146 default: break 147 } 148 } 149 150 @IBAction func unwindSelfTemplateList(segue: UIStoryboardSegue) { 151 152 } 153 154} 155コード

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/01/25 03:47 編集