swift
1コード 2import UIKit 3 4class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 5 6 7 @IBOutlet weak var tableView: UITableView! 8 9 var sectionTitleArray = [String]() 10 11 var dataArrayGroup: [[String]] = [] 12 13 override func viewDidLoad() { 14 super.viewDidLoad() 15 16 tableView.estimatedRowHeight = 44 17 tableView.rowHeight = UITableViewAutomaticDimension 18 tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell") 19 } 20 21 // Sectionを追加する(アニメーションはご自由に) 22 @IBAction func addSection(_ sender: Any) { 23 //func addSection() { 24 25 let alert = UIAlertController(title:"タイトル", 26 message: "メッセージ", 27 preferredStyle: .alert) 28 29 let cancelAction = UIAlertAction(title: "Cancel", 30 style: .cancel, 31 handler: 32 { action -> Void in 33 print("Cancel") 34 }) 35 36 let defaultAction = UIAlertAction(title: "OK", 37 style: .default, 38 handler: 39 { action -> Void in 40 41 // TextFieldから値を取得 42 if let textFields = alert.textFields { 43 for textField in textFields { 44 45 if let text = textField.text, !text.isEmpty { 46 47 // 取得したテキストをセクションのタイトルとして追加する 48 print(text) 49 50 self.sectionTitleArray.insert(text, at: 0) 51 self.dataArrayGroup.insert([], at: 0) 52 self.tableView.insertSections(IndexSet(integer: 0), with: .automatic) 53 } 54 } 55 } 56 }) 57 58 alert.addAction(cancelAction) 59 alert.addAction(defaultAction) 60 61 alert.addTextField(configurationHandler: { text -> Void in 62 63 }) 64 65 present(alert, animated: true, completion: nil) 66 67 } 68 69 70 // Rowを追加する(アニメーションはご自由に) 71 @IBAction func addRow(_ sender: Any) { 72 //func addRow() { 73 74 if dataArrayGroup.count == 0 { 75 return 76 } 77 78 let count = dataArrayGroup[0].count 79 let alert = UIAlertController(title:"タイトル", 80 message: "メッセージ", 81 preferredStyle: .alert) 82 83 let cancelAction = UIAlertAction(title: "Cancel", 84 style: .cancel, 85 handler: 86 { action -> Void in 87 print("Cancel") 88 }) 89 90 let defaultAction = UIAlertAction(title: "OK", 91 style: .default, 92 handler: 93 { action -> Void in 94 95 // TextFieldから値を取得 96 if let textFields = alert.textFields { 97 for textField in textFields { 98 99 if let text = textField.text, !text.isEmpty { 100 101 // 取得したテキストをセクションのタイトルとして追加する 102 print(text) 103 104 105 self.dataArrayGroup[0].insert(String(count), at: count) 106 self.tableView.insertRows(at: [IndexPath(row: count, section: 0)], with: .automatic) 107 } 108 } 109 } 110 }) 111 112 alert.addAction(cancelAction) 113 alert.addAction(defaultAction) 114 115 alert.addTextField(configurationHandler: { text -> Void in 116 117 }) 118 119 present(alert, animated: true, completion: nil) 120 } 121 122 123 // MARK: - TableView Delegate & DataSource 124 //この部分です。 125 func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 126 if sectionTitleArray.count == 0 { 127 return nil 128 } else { 129 return sectionTitleArray[section] 130 } 131 } 132 133 // Section Count 134 func numberOfSections(in tableView: UITableView) -> Int { 135 return dataArrayGroup.count 136 } 137 138 // Row Count 139 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 140 return dataArrayGroup[section].count 141 } 142 143 // Generate Cell 144 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 145 let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath as IndexPath) 146 let dataArray = dataArrayGroup[indexPath.section] 147 cell.textLabel?.text = dataArray[indexPath.row] 148 return cell 149 } 150 151 // Select Cell 152 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 153 tableView.deselectRow(at: indexPath as IndexPath, animated: true) 154 } 155 156 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 157 if editingStyle == UITableViewCellEditingStyle.delete { 158 dataArrayGroup.remove(at: indexPath.row) 159 tableView.deleteRows(at: [indexPath as IndexPath], with: UITableViewRowAnimation.fade) 160 } 161 } 162 163 //cellが削除が可能なことを伝える 164 func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle { 165 return UITableViewCellEditingStyle.delete; 166 } 167}
cellをdeleteのdeleteできるようにしたくdeleteの機能を追加したのですがエラーが起きてしまい自分なりに色々考えましたがよくわからなかったのでどこのコードが間違っているのかを知りたく質問させていただきました。
それとできればsectionもdelete出来るようにしたいので教えていただけたら嬉しいです
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/01/06 14:50
2017/01/06 14:56
2017/01/06 15:03
2017/01/06 15:13
2017/01/06 15:28
2017/01/06 15:30
2017/01/06 15:43
2017/01/07 03:35
2017/01/07 04:04
2017/01/07 04:13
2017/01/07 04:45
2017/01/07 05:01
2017/01/07 13:45
2017/01/07 16:07
2017/01/08 01:39
2017/01/08 09:59