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

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

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

TableView(UITableView)とは、リスト形式で表示するコントロールで、ほとんどのアプリに使用されています。画面を「行」に分けて管理し、一般的には各行をタップした際に詳細画面に移動します。

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回答

1833閲覧

tableview row(cell)

blakekei

総合スコア35

TableView

TableView(UITableView)とは、リスト形式で表示するコントロールで、ほとんどのアプリに使用されています。画面を「行」に分けて管理し、一般的には各行をタップした際に詳細画面に移動します。

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グッド

1クリップ

投稿2017/01/08 13:25

編集2017/01/09 14:29

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
テキストアラートテキストで入力出来るようにしたのですがエラーが出てしまったのでどの部分を書き直せばいいか知りたいです

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

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

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

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

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

guest

回答2

0

ベストアンサー

以下のリンクに簡単なサンプルを作りました、やりたいことはおそらくできていると思いますのでダウンロードして試してみてください。

サンプル teratail_61308

追記

s
d

投稿2017/01/08 14:00

編集2017/01/09 15:05
_Kentarou

総合スコア8490

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

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

blakekei

2017/01/08 15:06

何から何までありがとうございます とても勉強になりました また質問ができた際はお忙しいとは思いますがまたお願いします
blakekei

2017/01/09 13:42

一つ疑問なんですがcustomCell.swiftにlabelをドラッグ&ドロップをしよとしても出来ないのですがどうしたらいいですか?
_Kentarou

2017/01/09 13:47

サンプルのStoryboardのテーブルに乗っているセルにラベルを追加して、それをcustomCell.swiftに結びたいということですか?
blakekei

2017/01/09 13:54

はい、そうです viewcontrolle.swiftには結びつけれるのですがcustomCell.swiftになると結びつけれなくなります。
_Kentarou

2017/01/09 14:06

その状態で実行(ちゃんと表示)できますか?もしかするとセルのクラス名など変更してませんか?
_Kentarou

2017/01/09 14:12

パーツからドラッグ&ドロップして入力はできますか?入力できるのであればドラッグ&ドロップする場所が悪いかもなのでコードの一番下とかに結んで名前を付けてみてください。
blakekei

2017/01/09 14:41

特に何もいじってないのですが import UIKit class CustomCell: UITableViewCell { @IBOutlet weak var indexLabel: UILabel! @IBOutlet weak var countLabel: UILabel! @IBOutlet weak var priceLabel: UILabel! var data: DataModel! { didSet { countLabel.text = data.count priceLabel.text = data.price } } } このようにしようと思ってcustomcell.swiftにlabelをドラッグ&ドロップしても入力出来ないです うまく説明出来なくてすいません
_Kentarou

2017/01/09 14:49

Storyboardのセルに新しいラベルは追加してありますか?
_Kentarou

2017/01/09 14:56

Storyboardも私のプロジェクトのままですか?
blakekei

2017/01/09 14:56

写真貼りました このような感じなっています
blakekei

2017/01/09 15:02 編集

自分で新しく作ったプロジェクトです
_Kentarou

2017/01/09 15:06

回答に画像を貼りました、赤く囲ったところのが同じ様になっているか確認してください。
blakekei

2017/01/09 15:16

同じようになっていませんでした。 それと直したらドラッグ&ドロップして入力出来るようになりました お忙しい中、迅速に回答していただきありがとうございました。
blakekei

2017/02/06 06:33

以前Kentarouさんに教えていただいた内容を永続的に保存したくcoredataを書き加えたのですがどうしてもエラーが起きてしまいどうしても自力で解決できなくて困っています。 もし良かったらお手数ですがもう一度ご教示頂きたく存じます
blakekei

2017/02/07 14:57

しつこいですがをcoredataを利用してteratail_61308のデータを永続的に保存したいのですがやり方もしくは少しだけでもヒントを頂けませんか? お忙しいと思いますがよろしくお願いします
guest

0

このような感じでドラッグ&ドロップしても入力欄が出てきません
イメージ説明

投稿2017/01/09 14:53

blakekei

総合スコア35

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.51%

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

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

質問する

関連した質問