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

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

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

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

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

Swift

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

Q&A

解決済

2回答

1479閲覧

alerttextについて

blakekei

総合スコア35

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

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

Swift

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

0グッド

0クリップ

投稿2017/01/07 04:45

編集2017/01/07 16:04

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行まで用意できたのですが入力したデータが反映されないですがどうしたらいいでしょうか?

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

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

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

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

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

guest

回答2

0

ベストアンサー

こちらのコードでひとまず、アラートの入力が1行目と2行目も入力できます。
しかし各行に文字列を代入処理をしているため、片方の行を入力しても空白の欄が出力されます。
それと、同じ処理を記入しているself.tableView.reloadData()など1行でも良い書き方があるかもしれません。

完全に出来上がっているコードのみを渡すとAppStore申請まで行き着く場合は、逆に時間がかかります。
理由は全部聞かないとわからないからです。

機能をシンプルにして、対応方法を考えてみてほしいです。
それとループ処理なども調べ、理解、実装を続けると、よりできるようになると思いました。

// Rowを追加する(アニメーションはご自由に) @IBAction func addRow(_ sender: UIBarButtonItem) { // アラートビューにテキストフィールドを追加 let alertController = UIAlertController(title: "", message: "商品名と数字を入力してください", preferredStyle: .alert) alertController.addTextField(configurationHandler: nil) let otherAction = UIAlertAction(title: "OK", style: .default) { action in if let textFields = alertController.textFields { let textField = textFields[0] self.dataArrayGroup.append([textField.text!]) // 取得したテキストをセクションのタイトルとして追加する self.tableView.reloadData() } if let textFields = alertController.textFields { let textField = textFields[1] self.dataArrayGroup.append([textField.text!]) // 取得したテキストをセクションのタイトルとして追加する self.tableView.reloadData() } } alertController.addTextField { (textField1 : UITextField) -> Void in } alertController.addAction(otherAction) present(alertController, animated: true, completion: nil) }

あとすみません。毎回微妙にコードが変化しているので、実施しようとする事が余計に難しくなっています。
それと実施したい機能をもっとシンプルな実装にすると、考えやすいです。

例えばですが、、
マスターソースは全部の機能があるもの。
各機能ごとにコードを実装しているソースに分けると考えも、まとまりやすいと思います。
機能が出来上がったらマスターソースに結合していくとアプリ開発ももっと効率的になると思います。

自らのタイミングの良い時でもSourceTreeなどでソース管理というものを使えるようになると、一気にできることが増えるかもしれません。

投稿2017/01/07 18:13

編集2017/01/07 18:15
Dbank

総合スコア120

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

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

blakekei

2017/01/08 12:43 編集

こちらのコードだと func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { if sectionTitleArray.count == 0 { return nil } else { return sectionTitleArray[section]//ここの部分がエラー起きるのですがどうしたらいいですか? } }
Dbank

2017/01/09 09:58

コンソールのエラー文に英語でResonと書かれている場合は、そこの文章をコピーして翻訳して意味を知る。その英文をコピーしてネット検索をして同様なエラー文の解説がほとんどの場合はあるので、それをヒントに解決します。Resonが書かれていない場合は、文章的なところをひとつづつネットで調べますね。
guest

0

そちらのコードではelseの箇所が、{}など足りない箇所がありますので(他にもあるかもしれません。)、機能として必要な部分だけお伝えします。

実際に試したコードを記載します。

alert.addAction(cancelAction)の上にこちらを記載するとUITextFiledは、2行表示されます。

alertController.addTextField { (textField : UITextField) -> Void in //コード }

UIAlertControlelrの2行にする方法

Q
[(1)と(個×)と(円)]のところは自動的に追加できるようにしたいです。
A
action in の下に

if let textFields = alertController.textFields { let textFieldTwo = textFields[1] }

textFieldTwoの変数に1行目のTextFildを表示する方法と同様に実施すれば対応できます。以前もセクション内にデータを記載する方法を回答させて頂きましたが、1行目で文字のデータ入力を理解すれば、2行目も同様に実施できると思います。

投稿2017/01/07 15:30

Dbank

総合スコア120

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

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

blakekei

2017/01/07 15:59

action inってどの部分を指してますか? またrowの部分だけテキストアラートで入力してもデータが反映されないのですがどうしたいいでしょうか?
blakekei

2017/01/08 10:00

labelの内容がrowを追加した時に表示出来ないのはどこかにコードを書き足さなきゃいけないのでしょうか?
Dbank

2017/01/08 11:20

コードを書き足す前に、なぜlabelで入力した値が、画面に表示されると思いますか?この質問の答えが、コードを書き足す場合、コードを除く場合の答えになります。なので思考してください。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問