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
試したこと
検索してみましたが値を渡す最善の方法が見つからず、現在に至ります。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。