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

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

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

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

Q&A

0回答

148閲覧

TableViewCell(セクション分けした)にTextFieldの内容を反映させたい

KCROW

総合スコア7

Swift

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

0グッド

0クリップ

投稿2021/01/07 13:41

現在筋トレアプリを作成中です。

TableViewを用いてセクション分けし、

追加➕ボタン → 部位と種目を書き込む(部位はPickerViewで選択) → 登録ボタン → セクションに振り分けてセルに反映させたいです

イメージはこの通りです↓
イメージ説明

ViewController

1import UIKit 2 3class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource { 4 5 func numberOfSections(in tableView: UITableView) -> Int { 6 return mySections.count 7 } 8 9 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 10 return twoDimArray[Int(section)].count 11 12 13 } 14 15 func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 16 return mySections[Int(section)] 17 } 18 19 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 20 let cell = tableView.dequeueReusableCell(withIdentifier: "SampleCell", for: indexPath) 21 cell.textLabel?.text = twoDimArray[indexPath.section][Int(indexPath.row)] 22 return cell 23 } 24 25 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 26 selectedBui = mySections[indexPath.section] 27 selectedSyumoku = twoDimArray[indexPath.section][indexPath.row] 28 // Segueを使った画面遷移を行う関数 29 performSegue(withIdentifier: "Segue", sender: nil) 30 //選択後の背景色を消す 31 tableView.deselectRow(at: indexPath, animated: true) 32 } 33 34 35 var mySections = [String]() 36 var twoDimArray = [[String]]() 37 var selectedBui = "" 38 var selectedSyumoku = "" 39 var cellcount = 0 40 41 42 @IBOutlet weak var tableView: UITableView! 43 44 45 46 override func viewDidLoad() { 47 super.viewDidLoad() 48 49 mySections = ["肩","胸","背中","腕","脚","腹"] 50 51 for _ in 0 ... 5 { 52 twoDimArray.append([]) 53 } 54 twoDimArray[0] = ["ショルダープレス","サイドレイズ"] 55 twoDimArray[1] = ["ダンベルプレス","ダンベルフライ"] 56 twoDimArray[2] = ["オーバーロー","ラットプル"] 57 twoDimArray[3] = ["ハンマーカール","スカルクラッシャー"] 58 twoDimArray[4] = ["レッグプレス","レッグエクステンション"] 59 twoDimArray[5] = ["レッグレイズ"] 60 61 } 62 63 64 func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { 65 return true 66 } 67 68 //スワイプしたセルを削除 69 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) { 70 if editingStyle == UITableViewCell.EditingStyle.delete { 71 twoDimArray.remove(at: indexPath.row) 72 73 tableView.deleteRows(at: [indexPath as IndexPath], with: UITableView.RowAnimation.automatic) 74 } 75 76} 77} 78

PlusMenuViewController

1import UIKit 2 3 4class PlusMenuViewController: UIViewController,UIPickerViewDelegate,UIPickerViewDataSource{ 5 func numberOfComponents(in pickerView: UIPickerView) -> Int { 6 return 1 7 } 8 9 func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { 10 return 6 11 } 12 13 func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { 14 return sectionTitles[row] 15 } 16 17 func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { 18 buiTextField.text = sectionTitles[row] 19 } 20 21 @IBAction func Registration(_ sender: Any) { 22 23 24 _ = self.navigationController?.popViewController(animated: true) 25 26 print("kko") 27 } 28 29 30 var twoDimArray = [[String]]() 31 32 @IBOutlet weak var TableView: UITableView! 33 34 @IBOutlet weak var buiTextField: UITextField! 35 @IBOutlet weak var syumokuTextField: UITextField! 36 var pickerView = UIPickerView() 37 var sectionTitles = ["肩", "胸","背中","腕","足","腹"] 38 39 override func viewDidLoad() { 40 super.viewDidLoad() 41 42 createPickerView() 43 } 44 45 func createPickerView() { 46 pickerView.delegate = self 47 buiTextField.inputView = pickerView 48 49 } 50 51 @objc func donePicker() { 52 buiTextField.endEditing(true) 53 } 54 55 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 56 buiTextField.endEditing(true) 57 } 58 59 60 override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 61 syumokuTextField.endEditing(true) 62 } 63} 64 65 66

セクションを使わない追加方法は調べて見つけたのですが、
セクションを使用した場合がなかったので分かるかたいましたら教えていただきたいです。

よろしくお願いいたします。

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問