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

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

新規登録して質問してみよう
ただいま回答率
85.48%
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

解決済

2回答

1443閲覧

tableview section

blakekei

総合スコア35

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クリップ

投稿2016/12/31 16:32

swift

1import UIKit 2 3class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 4 5 6 @IBOutlet weak var tableView: UITableView! 7 8 var sectionTitleArray = ["Alphabet Num","Number","Alphabet"] 9 10 // Data Array 11 var dataArray1 = ["One","Two","Three","Four","Five"] 12 var dataArray2 = ["1","2","3","4","5"] 13 var dataArray3 = ["a","b","c","d","e"] 14 var dataArrayGroup: [[String]] = [] 15 16 override func viewDidLoad() { 17 super.viewDidLoad() 18 19 20 // Create Data 21 dataArrayGroup = [dataArray1, dataArray2, dataArray3] 22 23 tableView.estimatedRowHeight = 44 24 tableView.rowHeight = UITableViewAutomaticDimension 25 tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell") 26 } 27 28 // Sectionを追加する(アニメーションはご自由に) 29 @IBAction func addSection(_ sender: Any) { 30 //func addSection() { 31 32 //教わった部分を自分なりにこうして見たのですがアラートで入力したデータをセクションに反映させることが出来ませんでした 33 let addSectionTitle = "Number" 34 let addSectionArray = ["1","2","3","4","5"] 35 36 let alert = UIAlertController(title:"タイトル", 37 message: "メッセージ", 38 preferredStyle: .alert) 39 40 let cancelAction = UIAlertAction(title: "Cancel", 41 style: .cancel, 42 handler: 43 { action -> Void in 44 print("Cancel") 45 }) 46 47 let defaultAction = UIAlertAction(title: "OK", 48 style: .default, 49 handler: 50 { action -> Void in 51 52 // TextFieldから値を取得 53 if let textFields = alert.textFields { 54 for textField in textFields { 55 56 if let text = textField.text, !text.isEmpty { 57 58 // 取得したテキストをセクションのタイトルとして追加する 59 print(text) 60 } 61 } 62 } 63 }) 64 65 alert.addAction(cancelAction) 66 alert.addAction(defaultAction) 67 68 alert.addTextField(configurationHandler: { text -> Void in 69 70 }) 71 72 present(alert, animated: true, completion: nil) 73 74 sectionTitleArray.insert(addSectionTitle, at: 1) 75 dataArrayGroup.insert(addSectionArray, at: 1) 76 tableView.insertSections(IndexSet(integer: 1), with: .automatic) 77 78 } 79 80 81 // Rowを追加する(アニメーションはご自由に) 82 @IBAction func addRow(_ sender: Any) { 83 //func addRow() { 84 85 let count = dataArrayGroup[1].count 86 87 dataArrayGroup[1].insert(String(count + 1), at: count) 88 tableView.insertRows(at: [IndexPath(row: count, section: 1)], with: .automatic) 89 } 90 91 92 // MARK: - TableView Delegate & DataSource 93 94 func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 95 return sectionTitleArray[section] 96 } 97 98 // Section Count 99 func numberOfSections(in tableView: UITableView) -> Int { 100 return dataArrayGroup.count 101 } 102 103 // Row Count 104 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 105 return dataArrayGroup[section].count 106 } 107 108 // Generate Cell 109 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 110 let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath as IndexPath) 111 let dataArray = dataArrayGroup[indexPath.section] 112 cell.textLabel?.text = dataArray[indexPath.row] 113 return cell 114 } 115 116 // Select Cell 117 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 118 tableView.deselectRow(at: indexPath as IndexPath, animated: true) 119 } 120} 121 122

教わった部分を自分なりにこうして見たのですがアラートで入力したデータをセクションに反映させることが出来ませんでした
それと追加したrowはrowをタップした時にキーボードからデータを入力できるプログラムももしよかった教えて頂いたいです。
何回もしつこく質問してすいません

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

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

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

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

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

fuzzball

2016/12/31 16:41

解決していないのに「解決済み」にして新しい質問を立てないで下さい。
guest

回答2

0

ベストアンサー

前に提示したのはとりあえず表示しただけなので、以下の様にして追加してみてください。
しかしこれも適当な位置に挿入しているだけなのでちゃんと自分の挿入したい位置に設定してください。

Rowに対してもSectionと同じ事なので頑張ってみてください。

swift

1import UIKit 2 3class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 4 5 6 @IBOutlet weak var tableView: UITableView! 7 8 var sectionTitleArray = ["Alphabet Num","Number","Alphabet"] 9 10 // Data Array 11 var dataArray1 = ["One","Two","Three","Four","Five"] 12 var dataArray2 = ["1","2","3","4","5"] 13 var dataArray3 = ["a","b","c","d","e"] 14 var dataArrayGroup: [[String]] = [] 15 16 override func viewDidLoad() { 17 super.viewDidLoad() 18 19 20 // Create Data 21 dataArrayGroup = [dataArray1, dataArray2, dataArray3] 22 23 tableView.estimatedRowHeight = 44 24 tableView.rowHeight = UITableViewAutomaticDimension 25 tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell") 26 } 27 28 // Sectionを追加する(アニメーションはご自由に) 29 @IBAction func addSection(_ sender: UIButton) { 30 31 let addSectionArray = ["1","2","3","4","5"] 32 let alert = UIAlertController(title:"タイトル", 33 message: "メッセージ", 34 preferredStyle: .alert) 35 36 let cancelAction = UIAlertAction(title: "Cancel", 37 style: .cancel, 38 handler: 39 { action -> Void in 40 print("Cancel") 41 }) 42 43 let defaultAction = UIAlertAction(title: "OK", 44 style: .default, 45 handler: 46 { action -> Void in 47 48 // TextFieldから値を取得 49 if let textFields = alert.textFields { 50 for textField in textFields { 51 52 if let text = textField.text, !text.isEmpty { 53 54 // 取得したテキストをセクションのタイトルとして追加する 55 print(text) 56 57 self.sectionTitleArray.insert(text, at: 1) 58 self.dataArrayGroup.insert(addSectionArray, at: 1) 59 self.tableView.insertSections(IndexSet(integer: 1), with: .automatic) 60 } 61 } 62 } 63 }) 64 65 alert.addAction(cancelAction) 66 alert.addAction(defaultAction) 67 68 alert.addTextField(configurationHandler: { text -> Void in 69 70 }) 71 72 present(alert, animated: true, completion: nil) 73 } 74 75 76 // Rowを追加する(アニメーションはご自由に) 77 @IBAction func addRow(_ sender: UIButton) { 78 //func addRow() { 79 80 let count = dataArrayGroup[1].count 81 82 dataArrayGroup[1].insert(String(count + 1), at: count) 83 tableView.insertRows(at: [IndexPath(row: count, section: 1)], with: .automatic) 84 } 85 86 87 // MARK: - TableView Delegate & DataSource 88 89 func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 90 return sectionTitleArray[section] 91 } 92 93 // Section Count 94 func numberOfSections(in tableView: UITableView) -> Int { 95 return dataArrayGroup.count 96 } 97 98 // Row Count 99 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 100 return dataArrayGroup[section].count 101 } 102 103 // Generate Cell 104 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 105 let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath as IndexPath) 106 let dataArray = dataArrayGroup[indexPath.section] 107 cell.textLabel?.text = dataArray[indexPath.row] 108 return cell 109 } 110 111 // Select Cell 112 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 113 tableView.deselectRow(at: indexPath as IndexPath, animated: true) 114 } 115}

投稿2016/12/31 16:50

_Kentarou

総合スコア8490

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

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

blakekei

2017/01/08 12:24

何回も何回も質問してご迷惑だと思いますがどうしてもrowの部分を自分なりに書き換えて見てもデータが反映されないですがどこの部分を書き換えればいいですか? ご迷惑と重々承知ですがなんとか回答して頂けないですか?@IBAction func addRow(_ sender: Any) { //func addRow() { if dataArrayGroup.count == 0 { return } let count = dataArrayGroup[0].count let alert = UIAlertController(title:"タイトル", message: "メッセージ", preferredStyle: .alert) let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: { action -> Void in print("Cancel") }) let defaultAction = UIAlertAction(title: "OK", style: .default, handler: { action -> Void in // TextFieldから値を取得 if let textFields = alert.textFields { for textField in textFields { if let text = textField.text, !text.isEmpty { // 取得したテキストをセクションのタイトルとして追加する print(text) self.dataArrayGroup.insert([], at: 0) self.tableView.insertSections(IndexSet(integer: 0), with: .automatic) } } } }) alert.addAction(cancelAction) alert.addAction(defaultAction) alert.addTextField(configurationHandler: { text -> Void in alert.addTextField(configurationHandler: {text -> Void in }) }) present(alert, animated: true, completion: nil) } 自分で考えたコードがこれです。
guest

0

swift

1// 取得したテキストをセクションのタイトルとして追加する 2print(text)

printしてるだけだから追加されるわけもなく。
ここに追加処理を書いて下さい。

投稿2016/12/31 16:40

fuzzball

総合スコア16731

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問