swift
1コード 2import UIKit 3 4class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 5 6 7 @IBOutlet weak var tableView: UITableView! 8 9 var sectionTitleArray = [String]() 10 var dataArray1 = ["個×"] 11 var dataArray2 = ["円"] 12 var dataArrayGroup: [[String]] = [] 13 14 override func viewDidLoad() { 15 super.viewDidLoad() 16 17 dataArrayGroup = [dataArray1, dataArray2] 18 19 tableView.estimatedRowHeight = 44 20 tableView.rowHeight = UITableViewAutomaticDimension 21 tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell") 22 } 23 24 // Sectionを追加する(アニメーションはご自由に) 25 @IBAction func addSection(_ sender: Any) { 26 //func addSection() { 27 28 let alert = UIAlertController(title:"タイトル", 29 message: "メッセージ", 30 preferredStyle: .alert) 31 32 let cancelAction = UIAlertAction(title: "Cancel", 33 style: .cancel, 34 handler: 35 { action -> Void in 36 print("Cancel") 37 }) 38 39 let defaultAction = UIAlertAction(title: "OK", 40 style: .default, 41 handler: 42 { action -> Void in 43 44 // TextFieldから値を取得 45 if let textFields = alert.textFields { 46 for textField in textFields { 47 48 if let text = textField.text, !text.isEmpty { 49 50 // 取得したテキストをセクションのタイトルとして追加する 51 print(text) 52 53 self.sectionTitleArray.insert(text, at: 0) 54 self.dataArrayGroup.insert([], at: 0) 55 self.tableView.insertSections(IndexSet(integer: 0), with: .automatic) 56 } 57 } 58 } 59 }) 60 61 alert.addAction(cancelAction) 62 alert.addAction(defaultAction) 63 64 alert.addTextField(configurationHandler: { text -> Void in 65 66 }) 67 68 present(alert, animated: true, completion: nil) 69 70 } 71 72 @IBAction func deleteSection(_ sender: Any) { 73 74 if sectionTitleArray.isEmpty { 75 return 76 } 77 78 sectionTitleArray.remove(at: 0) 79 dataArrayGroup.remove(at: 0) 80 tableView.deleteSections(IndexSet(integer: 0), with: .automatic) 81 } 82 83 @IBOutlet weak var deletetext: UITextField! 84 85 // Rowを追加する(アニメーションはご自由に) 86 @IBAction func addRow(_ sender: Any) { 87 //func addRow() { 88 89 if dataArrayGroup.count == 0 { 90 return 91 } 92 93 let center = dataArray1.count //真ん中に自動的にデータが追加される 94 let right = dataArray2.count //右端に自動的にデータが追加せる 95 let count = dataArrayGroup[0].count //左端にrowが増える度に1.2.3とカウントされながらデータが追加される 96 self.dataArrayGroup[0].insert(String(count), at: count) 97 self.tableView.insertRows(at: [IndexPath(row: count, section: 0)], with: .automatic) 98 99 let alert = UIAlertController(title:"タイトル", 100 message: "メッセージ", 101 preferredStyle: .alert) 102 103 let cancelAction = UIAlertAction(title: "Cancel", 104 style: .cancel, 105 handler: 106 { action -> Void in 107 print("Cancel") 108 }) 109 110 let defaultAction = UIAlertAction(title: "OK", 111 style: .default, 112 handler: 113 { action -> Void in 114 115 // TextFieldから値を取得 116 if let textFields = alert.textFields { 117 else if textFields = alert.textFields { 118 for textField in textFields { 119 for textField in texFields { 120 121 } 122 123 if let text = textField.text, !text.isEmpty { 124 125 // 取得したテキストをセクションのタイトルとして追加する 126 print(text) 127 } 128 } 129 } 130 } 131 }) 132 alert.addTextField { (textField : UITextField) -> Void in 133 alert.addAction(cancelAction) 134 alert.addAction(defaultAction) 135 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}
自分の意図としてはrowがボタンによって追加され時に[1 3 個× 300 円]にしたくコードを書き換えて見たのですがエラーが起きました
[(3)と(300)]のところはアラートにテキスト2行を用意して入力出来るようにし、[(1)と(個×)と(円)]のところは自動的に追加できるようにしたいです。
alertを2行まで用意できたのですが入力したデータが反映されないですがどうしたらいいでしょうか?
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/01/08 12:43 編集
2017/01/09 09:58