swift
1コード 2import UIKit 3 4class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 5 6 7 @IBOutlet weak var label1: UILabel! 8 @IBOutlet weak var label2: UILabel! 9 @IBOutlet weak var tableView: UITableView! 10 11 var sectionTitleArray = [String]() 12 13 var dataArrayGroup: [[String]] = [] 14 15 16 override func viewDidLoad() { 17 super.viewDidLoad() 18 label1.text = "個×" 19 label2.text = "円" 20 tableView.estimatedRowHeight = 44 21 tableView.rowHeight = UITableViewAutomaticDimension 22 tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell") 23 } 24 25 // Sectionを追加する(アニメーションはご自由に) 26 @IBAction func addSection(_ sender: Any) { 27 //func addSection() { 28 29 let alert = UIAlertController(title:"タイトル", 30 message: "メッセージ", 31 preferredStyle: .alert) 32 33 let cancelAction = UIAlertAction(title: "Cancel", 34 style: .cancel, 35 handler: 36 { action -> Void in 37 print("Cancel") 38 }) 39 40 let defaultAction = UIAlertAction(title: "OK", 41 style: .default, 42 handler: 43 { action -> Void in 44 45 // TextFieldから値を取得 46 if let textFields = alert.textFields { 47 for textField in textFields { 48 49 if let text = textField.text, !text.isEmpty { 50 51 // 取得したテキストをセクションのタイトルとして追加する 52 print(text) 53 54 self.sectionTitleArray.insert(text, at: 0) 55 self.dataArrayGroup.insert([], at: 0) 56 self.tableView.insertSections(IndexSet(integer: 0), with: .automatic) 57 } 58 } 59 } 60 }) 61 62 alert.addAction(cancelAction) 63 alert.addAction(defaultAction) 64 65 alert.addTextField(configurationHandler: { text -> Void in 66 67 }) 68 69 present(alert, animated: true, completion: nil) 70 71 } 72 73 @IBAction func deleteSection(_ sender: Any) { 74 75 guard let text = deletetext.text, 76 !text.isEmpty, 77 !sectionTitleArray.isEmpty, 78 let indext = sectionTitleArray.index(of: text) else { 79 return 80 } 81 sectionTitleArray.remove(at: indext) 82 dataArrayGroup.remove(at: indext) 83 tableView.deleteSections(IndexSet(integer: indext), with: .automatic) 84 } 85 86 @IBOutlet weak var deletetext: UITextField! 87 88 // Rowを追加する(アニメーションはご自由に) 89 @IBAction func addRow(_ sender: Any) { 90 //func addRow() { 91 let count = dataArrayGroup[1].count 92 93 dataArrayGroup[1].insert(String(count + 1), at: count) 94 tableView.insertRows(at: [IndexPath(row: count, section: 1)], with: .automatic) 95 96 if dataArrayGroup.count == 0 { 97 return 98 } 99 100 //let count = dataArrayGroup[0].count 101 let alert = UIAlertController(title:"タイトル", 102 message: "メッセージ", 103 preferredStyle: .alert) 104 105 let cancelAction = UIAlertAction(title: "Cancel", 106 style: .cancel, 107 handler: 108 { action -> Void in 109 print("Cancel") 110 }) 111 112 let defaultAction = UIAlertAction(title: "OK", 113 style: .default, 114 handler: 115 { action -> Void in 116 117 // TextFieldから値を取得 118 if let textFields = alert.textFields { 119 for textField in textFields { 120 121 if let text = textField.text, !text.isEmpty { 122 123 // 取得したテキストをセクションのタイトルとして追加する 124 print(text) 125 126 self.tableView.reloadData() 127 } 128 } 129 } 130 }) 131 132 alert.addAction(cancelAction) 133 alert.addAction(defaultAction) 134 135 alert.addTextField(configurationHandler: { text -> Void in 136 alert.addTextField(configurationHandler: {text -> Void in 137 }) 138 }) 139 140 present(alert, animated: true, completion: nil) 141 } 142 143 144 145 // MARK: - TableView Delegate & DataSource 146 //この部分です。 147 func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 148 if sectionTitleArray.count == 0 { 149 return nil 150 } else { 151 return sectionTitleArray[section] 152 } 153 } 154 155 // Section Count 156 func numberOfSections(in tableView: UITableView) -> Int { 157 return dataArrayGroup.count 158 } 159 160 // Row Count 161 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 162 return dataArrayGroup[section].count 163 } 164 165 // Generate Cell 166 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 167 let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath as IndexPath) 168 let dataArray = dataArrayGroup[indexPath.section] 169 cell.textLabel?.text = dataArray[indexPath.row] 170 return cell 171 } 172 173 // Select Cell 174 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 175 tableView.deselectRow(at: indexPath as IndexPath, animated: true) 176 } 177 178 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 179 if editingStyle == UITableViewCellEditingStyle.delete { 180 dataArrayGroup[indexPath.section].remove(at: indexPath.row) 181 tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic) 182 } 183 } 184 185 //cellが削除が可能なことを伝える 186 func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle { 187 return UITableViewCellEditingStyle.delete; 188 } 189}
コード let count = dataArrayGroup[1].count dataArrayGroup[1].insert(String(count + 1), at: count) tableView.insertRows(at: [IndexPath(row: count, section: 1)], with: .automatic)
これで上記のコードでrowを追加される度に1,2,3...となっていって
labelは
label1.text = "個×"
label2.text = "円"
このような感じで
あとは赤のテキストフィールドと青のテキストフィールドは
alert.addTextField(configurationHandler: { text -> Void in
alert.addTextField(configurationHandler: {text -> Void in
テキストアラートテキストで入力出来るようにしたのですがエラーが出てしまったのでどの部分を書き直せばいいか知りたいです
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/01/08 15:06
2017/01/09 13:42
2017/01/09 13:47
2017/01/09 13:54
2017/01/09 14:06
2017/01/09 14:12
2017/01/09 14:41
2017/01/09 14:49
2017/01/09 14:56
2017/01/09 14:56
2017/01/09 15:02 編集
2017/01/09 15:06
2017/01/09 15:16
2017/02/06 06:33
2017/02/07 14:57