tablevie でボタンをタップするとsectionが増えるプログラムを組みたいです
それともう一つのボタンでタップするとrowが増えるプログラムを組みたいです
Swift3,xcode8でほプログラムでお願いします
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
回答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
総合スコア8490
あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/12/30 13:34