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

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

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

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

Swift

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

Q&A

解決済

1回答

1064閲覧

tablevie section headerを増やす

blakekei

総合スコア35

Xcode

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

Swift

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

0グッド

0クリップ

投稿2016/12/30 02:35

tablevie でボタンをタップするとsectionが増えるプログラムを組みたいです
それともう一つのボタンでタップするとrowが増えるプログラムを組みたいです
Swift3,xcode8でほプログラムでお願いします

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

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

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

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

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

guest

回答1

0

ベストアンサー

最低限のセクション分けのあるテーブルのコードを載せました、addSection()addRow()メソッドのところに質問されているコードを書いたので参考にしてみてください。

swift

1import UIKit 2 3class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 4 5 @IBOutlet weak var tableView: UITableView! 6 7 var sectionTitleArray = ["Alphabet Num","Number","Alphabet"] 8 9 // Data Array 10 var dataArray1 = ["One","Two","Three","Four","Five"] 11 var dataArray2 = ["1","2","3","4","5"] 12 var dataArray3 = ["a","b","c","d","e"] 13 var dataArrayGroup: [[String]] = [] 14 15 override func viewDidLoad() { 16 super.viewDidLoad() 17 18 // Create Data 19 dataArrayGroup = [dataArray1, dataArray2, dataArray3] 20 21 tableView.estimatedRowHeight = 44 22 tableView.rowHeight = UITableViewAutomaticDimension 23 tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell") 24 } 25 26 // Sectionを追加する(アニメーションはご自由に) 27 func addSection() { 28 29 let addSectionTitle = "Number" 30 let addSectionArray = ["1","2","3","4","5"] 31 32 sectionTitleArray.insert(addSectionTitle, at: 1) 33 dataArrayGroup.insert(addSectionArray, at: 1) 34 tableView.insertSections(IndexSet(integer: 1), with: .automatic) 35 } 36 37 // Rowを追加する(アニメーションはご自由に) 38 func addRow() { 39 40 let count = dataArrayGroup[1].count 41 42 dataArrayGroup[1].insert(String(count + 1), at: count) 43 tableView.insertRows(at: [IndexPath(row: count, section: 1)], with: .automatic) 44 } 45 46 // MARK: - TableView Delegate & DataSource 47 48 func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 49 return sectionTitleArray[section] 50 } 51 52 // Section Count 53 func numberOfSections(in tableView: UITableView) -> Int { 54 return dataArrayGroup.count 55 } 56 57 // Row Count 58 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 59 return dataArrayGroup[section].count 60 } 61 62 // Generate Cell 63 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 64 let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath as IndexPath) 65 let dataArray = dataArrayGroup[indexPath.section] 66 cell.textLabel?.text = dataArray[indexPath.row] 67 return cell 68 } 69 70 // Select Cell 71 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 72 tableView.deselectRow(at: indexPath as IndexPath, animated: true) 73 } 74}

投稿2016/12/30 05:59

_Kentarou

総合スコア8490

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

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

blakekei

2016/12/30 13:34

ありがとうございます 参考になります それともしよかったらボタンをタップした時にテキストアラートが表示されてそこで名前をつけて追加するプログラムももしお分りでしたら教えていただけませんか?
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問