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

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

解決済

1回答

1495閲覧

tablevie section row について

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

投稿2017/01/01 04:17

編集2017/01/01 07:32

swift

1コードimport 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 let addSectionArray = [String]() 33 let alert = UIAlertController(title:"タイトル", 34 message: "メッセージ", 35 preferredStyle: .alert) 36 37 let cancelAction = UIAlertAction(title: "Cancel", 38 style: .cancel, 39 handler: 40 { action -> Void in 41 print("Cancel") 42 }) 43 44 let defaultAction = UIAlertAction(title: "OK", 45 style: .default, 46 handler: 47 { action -> Void in 48 49 // TextFieldから値を取得 50 if let textFields = alert.textFields { 51 for textField in textFields { 52 53 if let text = textField.text, !text.isEmpty { 54 55 // 取得したテキストをセクションのタイトルとして追加する 56 print(text) 57 58 self.sectionTitleArray.insert(text, at: 1) 59 self.dataArrayGroup.insert(addSectionArray, at: 1) 60 self.tableView.insertSections(IndexSet(integer: 1), with: .automatic) 61 } 62 } 63 } 64 }) 65 66 alert.addAction(cancelAction) 67 alert.addAction(defaultAction) 68 69 alert.addTextField(configurationHandler: { text -> Void in 70 71 }) 72 73 present(alert, animated: true, completion: nil) 74 75 } 76 77 78 // Rowを追加する(アニメーションはご自由に) 79 @IBAction func addRow(_ sender: Any) { 80 //func addRow() { 81 82 let count = [String]().count 83 let alert = UIAlertController(title:"タイトル", 84 message: "メッセージ", 85 preferredStyle: .alert) 86 87 let cancelAction = UIAlertAction(title: "Cancel", 88 style: .cancel, 89 handler: 90 { action -> Void in 91 print("Cancel") 92 }) 93 94 let defaultAction = UIAlertAction(title: "OK", 95 style: .default, 96 handler: 97 { action -> Void in 98 99 // TextFieldから値を取得 100 if let textFields = alert.textFields { 101 for textField in textFields { 102 103 if let text = textField.text, !text.isEmpty { 104 105 // 取得したテキストをセクションのタイトルとして追加する 106 print(text) 107 108 self.dataArrayGroup[1].insert(String(count + 1), at: count) 109 self.tableView.insertRows(at: [IndexPath(row: count, section: 1)], with: .automatic) 110 } 111 } 112 } 113 }) 114 115 alert.addAction(cancelAction) 116 alert.addAction(defaultAction) 117 118 alert.addTextField(configurationHandler: { text -> Void in 119 120 }) 121 122 present(alert, animated: true, completion: nil) 123 } 124 125 126 // MARK: - TableView Delegate & DataSource 127 128 func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 129 return sectionTitleArray[section] 130 } 131 132 // Section Count 133 func numberOfSections(in tableView: UITableView) -> Int { 134 return dataArrayGroup.count 135 } 136 137 // Row Count 138 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 139 return dataArrayGroup[section].count 140 } 141 142 // Generate Cell 143 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 144 let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath as IndexPath) 145 let dataArray = dataArrayGroup[indexPath.section] 146 cell.textLabel?.text = dataArray[indexPath.row] 147 return cell 148 } 149 150 // Select Cell 151 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 152 tableView.deselectRow(at: indexPath as IndexPath, animated: true) 153 } 154} 155

教えて頂いたセクションの部分は改善することが出来ました
それとテキストアラートでrowの長方形の空白の部分以外はrowが追加される度に表示され空白の部分だけテキストアラートで入力できるプログラムを教えて頂きたいです
イメージとしては
1 りんご2 個 × 100 円
2 なし3 個 × 400 円
みたいな感じです
![イメージ説明]

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

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

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

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

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

guest

回答1

0

ベストアンサー

こんにちは。ソースを拝見させていただきました。よろしくお願いします。
1.こちらの文字データを表示するには。
var sectionTitleArray = ["Alphabet Num","Number","Alphabet"]
// Data Array
var dataArray1 = ["One","Two","Three","Four","Five"]
var dataArray2 = ["1","2","3","4","5"]
var dataArray3 = ["a","b","c","d","e"]

viewDidLoadの中でtableView.dataSource = selfとtableView.delegate = selfの記載が必要です。

このように記述願います。
override func viewDidLoad() {
super.viewDidLoad()

tableView.dataSource = self tableView.delegate = self

..
..
..
}

2.文字列をアラートのUItextFiledで入力後、Viewに反映するには、幾つかの方法があります。Xcode内のViewController内のソースを適当にクリックして、コマンド+Fを押して、検索ワードの箇所に、
presentと検索すると下記のメソッド記述がありますので、こちらで self.tableView.reloadData()と記載すると表示されます。ちなみにTableViewのリロードなどで調べるとネット情報は結構あります。
present(alert, animated: true, completion: nil)
self.tableView.reloadData()

3.セクションについて、現在は明示的に self.dataArrayGroup[1].insert(String(text), at: count)
self.tableView.insertRows(at: [IndexPath(row: count, section: 1)], with: .automatic)と記述されています。セクションは0から始まるので、1は真ん中のデータが増える仕組みになります。

セクションで入力を変化させる場合も幾つか方法があると思いますが、アラートの表示の際にセクションの入力番号を分岐させる方法がありますが、セクションでデータ入力の前に、セクションなしのtableViewの仕組みを把握すると分かりやすくなるかもしれません。
参考リンク Swift3 TableView

またTableViewのnumberOfRowsInSection などメソッド機能を調べながら進めることをお勧めいたします。

投稿2017/01/01 10:48

編集2017/01/01 15:35
Dbank

総合スコア120

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問