質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
TableView

TableView(UITableView)とは、リスト形式で表示するコントロールで、ほとんどのアプリに使用されています。画面を「行」に分けて管理し、一般的には各行をタップした際に詳細画面に移動します。

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

1496閲覧

ボタンの追加

退会済みユーザー

退会済みユーザー

総合スコア0

TableView

TableView(UITableView)とは、リスト形式で表示するコントロールで、ほとんどのアプリに使用されています。画面を「行」に分けて管理し、一般的には各行をタップした際に詳細画面に移動します。

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2017/03/06 16:19

編集2017/03/10 09:00

イメージ説明
実現したいこと
上の手書きで書いたイメージ図みたいにadd sectionでセクションを追加した時にsectionと他にその下にrowを追加できるボタンを備え付けたいです
そしてrowをタップ時にアラートテキストで入力できるようにしたいです

イメージ説明

試してみたこと
自分ではとあるgithubを参考にしてsectionを追加するボタンと削除できるボタンを作ったのですがrowを追加するボタンの作り方が分からなくて色々試してみたのですがどうしても思いつかないので教えていただけたら嬉しいです。

//編集・追加

swift

1import UIKit 2 3class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 4 5 6 @IBOutlet weak var tableView: UITableView! 7 8 var sectionTitleArray = [String]() 9 var dataArray1 = ["add Row"] 10 var dataArrayGroup: [[DataModel]] = [] 11 12 13 override func viewDidLoad() { 14 super.viewDidLoad() 15 16 tableView.estimatedRowHeight = 44 17 tableView.rowHeight = UITableViewAutomaticDimension 18 tableView.tableFooterView = UIView() 19 } 20 21 22 23 @IBAction func addSection(_ sender: UIButton) { 24 25 let alert = UIAlertController(title:"Sction追加", 26 message: "SectionTitleを入力してください", 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 alert.addTextField(configurationHandler: { text -> Void in }) 61 present(alert, animated: true, completion: nil) 62 63 //addrowボタン 64 let count = dataArrayGroup[1].count 65 66 dataArray1[1].insert([], at: 0) 67 tableView.insertRows(at: [IndexPath(row: count, section: 1)], with: .automatic) 68 } 69 70 @IBAction func deleteSection(_ sender: UIButton) { 71 72 if sectionTitleArray.isEmpty { 73 return 74 } 75 76 let alert = UIAlertController(title:"Sction削除", 77 message: "削除するSectionTitleを入力してください", 78 preferredStyle: .alert) 79 80 let cancelAction = UIAlertAction(title: "Cancel", 81 style: .cancel, 82 handler: 83 { action -> Void in 84 print("Cancel") 85 }) 86 87 let defaultAction = UIAlertAction(title: "OK", 88 style: .default, 89 handler: 90 { action -> Void in 91 92 // TextFieldから値を取得 93 if let textFields = alert.textFields { 94 for textField in textFields { 95 96 if let text = textField.text, 97 !text.isEmpty, 98 let index = self.sectionTitleArray.index(of: text) { 99 100 self.sectionTitleArray.remove(at: index) 101 self.dataArrayGroup.remove(at: index) 102 self.tableView.deleteSections(IndexSet(integer: index), with: .automatic) 103 } 104 } 105 } 106 }) 107 108 alert.addAction(cancelAction) 109 alert.addAction(defaultAction) 110 alert.addTextField(configurationHandler: { text -> Void in }) 111 present(alert, animated: true, completion: nil) 112 113 114 } 115 116 // Rowを追加する(アニメーションはご自由に) 117 @IBAction func addRow(_ sender: UIButton) { 118 //func addRow() { 119 120 if dataArrayGroup.count == 0 { 121 return 122 } 123 124 let alert = UIAlertController(title:"Row追加", 125 message: "個数と金額を入力してください", 126 preferredStyle: .alert) 127 128 let cancelAction = UIAlertAction(title: "Cancel", 129 style: .cancel, 130 handler: 131 { action -> Void in 132 print("Cancel") 133 }) 134 135 let count = dataArrayGroup[0].count 136 let defaultAction = UIAlertAction(title: "OK", 137 style: .default, 138 handler: 139 { action -> Void in 140 141 // TextFieldから値を取得 142 if let textFields = alert.textFields { 143 144 let data = DataModel() 145 146 for textField in textFields { 147 148 if let text = textField.text, !text.isEmpty, let _ = Int(text) { 149 if textField.tag == 1 { 150 data.count = text 151 } else { 152 data.price = text 153 } 154 } 155 } 156 157 self.dataArrayGroup[0].insert(data, at: count) 158 self.tableView.insertRows(at: [IndexPath(row: count, section: 0)], with: .automatic) 159 } 160 }) 161 162 163 alert.addTextField { 164 $0.placeholder = "個数" 165 $0.keyboardType = .numberPad 166 $0.tag = 1 167 } 168 alert.addTextField { 169 $0.placeholder = "金額" 170 $0.keyboardType = .numberPad 171 $0.tag = 2 172 } 173 174 alert.addAction(cancelAction) 175 alert.addAction(defaultAction) 176 present(alert, animated: true, completion: nil) 177 } 178 179 180 181 // MARK: - TableView Delegate & DataSource 182 //この部分です。 183 func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 184 if sectionTitleArray.count == 0 { 185 return nil 186 } else { 187 return sectionTitleArray[section] 188 } 189 } 190 191 // Section Count 192 func numberOfSections(in tableView: UITableView) -> Int { 193 return dataArrayGroup.count 194 } 195 196 // Row Count 197 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 198 return dataArrayGroup[section].count 199 } 200 201 // Generate Cell 202 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 203 let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell 204 cell.data = dataArrayGroup[indexPath.section][indexPath.row] 205 cell.indexLabel.text = String(indexPath.row + 1) 206 return cell 207 } 208 209 // Select Cell 210 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 211 tableView.deselectRow(at: indexPath as IndexPath, animated: true) 212 } 213 214 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 215 if editingStyle == UITableViewCellEditingStyle.delete { 216 dataArrayGroup[indexPath.section].remove(at: indexPath.row) 217 tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic) 218 } 219 } 220 221 //cellが削除が可能なことを伝える 222 func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle { 223 return UITableViewCellEditingStyle.delete; 224 } 225}

参考文献
KentarouさんのGitHub

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

Rowの数をデータの配列数+1にして配列の最後をRowを追加するセルにする事で解決できると思いますよ。

投稿2017/03/06 23:42

編集2017/03/06 23:43
_Kentarou

総合スコア8490

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2017/03/10 09:06

回答していただいてありがとうございます アドバイスを参考にしてとりあえずsection追加時にrowも同時に一行追加できるようにしようと試みてみたのですがどうしてもうまく行かなかったのでもう少し具体的に教えていただけないでしょうか?
退会済みユーザー

退会済みユーザー

2017/05/28 15:40

// Row Count func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return dataArrayGroup[section].count+1 すいませんマナー違反だと思うのすが再度どうしてもお聞きしたく質問させていただきました rowの配列+1とはこのことでしょうか?また配列の最後のrowを追加とはどういうことでしょうか? お手数ですが教えていただきたいです。
退会済みユーザー

退会済みユーザー

2017/05/30 23:05

もしよろしければ「tableview cellのいろいろ」の質問について教えていただけないでしょうか? ここ数日間自分なりに考えていろいろ工夫してみたのですがどうしてもうまく出来ないので教えて頂けたら幸いです。 ぜひお願いします。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問