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

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

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

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

Xcode

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

Swift

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

Q&A

解決済

1回答

1283閲覧

TableViewでの値渡し (CustomCell)

LATA-apple

総合スコア3

TableView

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

Xcode

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

Swift

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

0グッド

0クリップ

投稿2021/03/12 09:24

TableView (CustomCell)での値渡し

CustomCellでTableViewを作ったのですが、セルごとに各項目で違う値を表示したいため値渡しが必要なのですが、実行すると値が渡っておらず、対象のTableViewを表示できません。
独学でプログラミングを始めて1ヶ月ほどなのでわかりやすく解決方法を教えてくださると嬉しいです。

発生している問題・エラーメッセージ

Swift

1エラーメッセージなし

該当のソースコード

Swift

1// 2// iPhoneMenu_ViewController.swift 3// NewsReader 4// 5// Created by LATA on 2021/02/07. 6// 7 8import UIKit 9 10class iPhoneMenu_ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 11 12 var iPhoneName = ["iPhone(初代)","iPhone3G","iPhone3GS","iPhone4","iPhone4s","iPhone5","iPhone5c","iPhone5s","iPhone6","iPhone6 Plus","iPhone6s","iPhone6s Plus","iPhoneSE","iPhone7","iPhone7 Plus","iPhone8","iPhone8 Plus","iPhoneX","iPhoneXS","iPhoneXS Max","iPhoneXR","iPhone11","iPhone11Pro","iPhone11Pro Max","iPhoneSE(2nd)","iPhone12mini","iPhone12","iPhone12Pro","iPhone12Pro Max"] 13 var iPhoneDate = ["2007/06/29 (金)","2008/07/11 (金)","2009/06/26 (金)","2010/06/24 (木)","2011/10/14 (金)","2012/09/21 (金)","2013/09/20 (金)","2013/09/20 (金)","2014/09/19 (金)","2014/09/19 (金)","2015/09/25 (金)","2015/09/25 (金)","2016/03/31 (木)","2016/09/16 (金)","2016/09/16 (金)","2017/09/22 (金)","2017/09/22 (金)","2017/11/03 (金)","2018/09/21 (金)","2018/09/21 (金)","2018/10/26 (金)","2019/09/20 (金)","2019/09/20 (金)","2019/09/20 (金)","2020/04/24 (金)","2020/11/13 (金)","2020/10/23 (金)","2020/10/23 (金)","2020/11/13 (金)"] 14 var iPhoneImage = ["iPhone","iPhone3G","iPhone3GS","iPhone4","iPhone4s","iPhone5","iPhone5c","iPhone5s","iPhone6","iPhone6 Plus","iPhone6s","iPhone6s Plus","iPhoneSE (第一世代)","iPhone7","iPhone7 Plus","iPhone8","iPhone8 Plus","iPhoneX","iPhoneXS","iPhoneXS Max","iPhoneXR","iPhone11","iPhone11Pro","iPhone11Pro Max","iPhoneSE (第二世代)","iPhone12mini","iPhone12","iPhone12Pro","iPhone12Pro Max"] 15 16 17 @IBOutlet weak var tableView: UITableView! 18 19 override func viewDidLoad() { 20 super.viewDidLoad() 21 tableView.dataSource = self 22 tableView.delegate = self 23 24 // Do any additional setup after loading the view. 25 26 tableView.register(UINib(nibName: "iPhoneTableViewCell", bundle: nil), forCellReuseIdentifier: "iPhonecustomCell") 27 } 28 29 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 30 return 60 31 } 32 33 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 34 return iPhoneName.count 35 } 36 37 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 38 let cell = tableView.dequeueReusableCell(withIdentifier: "iPhonecustomCell", for: indexPath) as! iPhoneTableViewCell 39 cell.iPhoneName.text = iPhoneName[indexPath.row] 40 cell.iPhoneDate.text = iPhoneDate[indexPath.row] 41 cell.iPhoneImage.image = UIImage(named: iPhoneImage[indexPath.row]) 42 return cell 43 } 44 45 46 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 47 //画面が表示された時にハイライトを消す(ハイライトが徐々に消える) 48 if let indexPathForSelectedRow = tableView.indexPathForSelectedRow { 49 tableView.deselectRow(at: indexPathForSelectedRow, animated: true) 50 } 51 52 performSegue(withIdentifier: "toiPhone", sender: indexPath.row) 53 54 // performSegue(withIdentifier: "toTestView", sender: nil) 55 56 } 57 58 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 59 if segue.identifier == "toiPhone" { 60 if let row = tableView.indexPathForSelectedRow?.row { 61 let vc = segue.destination as! iPhoneViewController 62 vc.Sendtext = "(iPhoneName[row])" 63 print(iPhoneName[row]) 64 } 65 } 66 67 } 68 69 70 var alertController: UIAlertController! 71 func alert(title:String, message:String) { 72 alertController = UIAlertController(title: title, 73 message: message, 74 preferredStyle: .alert) 75 alertController.addAction(UIAlertAction(title: "OK", 76 style: .default, 77 handler: nil)) 78 present(alertController, animated: true) 79 } 80 81 func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { 82 83 // コピー1のアクションを設定する 84 let copy1Action = UIContextualAction(style: .normal , title: "製品名\nコピー") { [self] 85 (ctxAction, view, completionHandler) in 86 UIPasteboard.general.string = iPhoneName[indexPath.row] 87 completionHandler(true) 88 alert(title: "製品名コピー",message: "製品名をコピーしました。") 89 } 90 // コピー1のデザインを設定する 91 let copy1Image = UIImage(contentsOfFile: "製品名\nコピー")?.withTintColor(UIColor.white, renderingMode: .alwaysTemplate) 92 copy1Action.image = copy1Image 93 copy1Action.backgroundColor = UIColor(red: 0/255, green: 125/255, blue: 255/255, alpha: 1) 94 95 96 // コピー2のアクションを設定する 97 let copy2Action = UIContextualAction(style: .normal, title:"発売日\nコピー") { [self] 98 (ctxAction, view, completionHandler) in 99 UIPasteboard.general.string = iPhoneDate[indexPath.row] 100 completionHandler(true) 101 alert(title: "発表日コピー",message: "発表日をコピーしました。") 102 103 } 104 // コピー2のデザインを設定する 105 let copy2Image = UIImage(contentsOfFile:"発売日\nコピー")?.withTintColor(UIColor.white , renderingMode: .alwaysTemplate) 106 copy2Action.image = copy2Image 107 copy2Action.backgroundColor = UIColor(red: 255/255, green: 0/255, blue: 0/255, alpha: 1) 108 109 110 // コピー3のアクションを設定する 111 let copy3Action = UIContextualAction(style: .normal , title: "全て\nコピー") { [self] 112 (ctxAction, view, completionHandler) in 113 UIPasteboard.general.string = "(iPhoneName[indexPath.row]) (iPhoneDate[indexPath.row])" 114 completionHandler(true) 115 alert(title: "製品名/発表日コピー",message: "製品名/発表日をコピーしました。") 116 } 117 // コピー3のデザインを設定する 118 let copy3Image = UIImage(contentsOfFile: "全て\nコピー")?.withTintColor(UIColor.white, renderingMode: .alwaysTemplate) 119 copy3Action.image = copy3Image 120 copy3Action.backgroundColor = UIColor(red: 0/255, green: 225/255, blue: 0/255, alpha: 1) 121 122 // スワイプでの削除を無効化して設定する 123 let swipeAction = UISwipeActionsConfiguration(actions:[copy3Action, copy2Action, copy1Action]) 124 swipeAction.performsFirstActionWithFullSwipe = false 125 126 127 return swipeAction 128 129 } 130 131 132 /* 133 // MARK: - Navigation 134 135 // In a storyboard-based application, you will often want to do a little preparation before navigation 136 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 137 // Get the new view controller using segue.destination. 138 // Pass the selected object to the new view controller. 139 } 140 */ 141 142} 143

試したこと

検索してみましたが値を渡す最善の方法が見つからず、現在に至ります。

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

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

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

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

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

guest

回答1

0

ベストアンサー

Swift

1 tableView.deselectRow(at: indexPathForSelectedRow, animated: true)

これを実行すると、tableView.indexPathForSelectedRow?nil になってしまします。

一方、

Swift

1 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 2 3 performSegue(withIdentifier: "toiPhone", sender: indexPath.row) 4 }

このような感じで performSegue(withIdentifier:sender:)の第二引数で indexPath.row を渡すのであれば、

Swift

1 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 2 if segue.identifier == "toiPhone" { 3 if let row = sender as? Int { 4 print(iPhoneName[row]) 5 } 6 } 7 }

こんな感じで prepare(for:sender:)で渡してあげたindexPath.row (実質Int)を受けてあげればいいかと思います。

投稿2021/03/12 09:48

TsukubaDepot

総合スコア5086

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問