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をタップした時にキーボードからデータを入力できるプログラムももしよかった教えて頂いたいです。
何回もしつこく質問してすいません
解決していないのに「解決済み」にして新しい質問を立てないで下さい。
回答2件
あなたの回答
tips
プレビュー