###実現したいこと
課題リストを作っています
セルのタップで画面遷移をして詳細の情報を表示したいのですが
画面遷移の時にIndexPathを値渡しをできないのでしょうか?
課題名、課題期日は配列で管理しています
必要な場合はソースを提示します
質問があればお願いします
簡単ですがよろしくお願いします
参考にしたサイトはブックマーク等していなかったためわかりません。
すみません
swift
1import UIKit 2 3class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 4 5 @IBOutlet var tableView: UITableView! 6 var todoarray = [String]() 7 var todoarraydate = [String]() 8 var todocontent = [String]() 9 var refresher: UIRefreshControl? 10 11 override func viewDidLoad() { 12 super.viewDidLoad() 13 if UserDefaults.standard.array(forKey: "todoarray") == nil { 14 todoarray = [String]() 15 UserDefaults.standard.set(todoarray, forKey: "todoarray") 16 } else { 17 todoarray = UserDefaults.standard.array(forKey: "todoarray") as! [String] 18 } 19 20 if UserDefaults.standard.array(forKey: "todoarraydate") == nil { 21 todoarraydate = [String]() 22 UserDefaults.standard.set(todoarraydate, forKey: "todoarraydate") 23 } else { 24 todoarraydate = UserDefaults.standard.array(forKey: "todoarraydate") as! [String] 25 } 26 27 if UserDefaults.standard.array(forKey: "todocontent") == nil { 28 todocontent = [String]() 29 UserDefaults.standard.set(todocontent, forKey: "todocontent") 30 } else { 31 todocontent = UserDefaults.standard.array(forKey: "todocontent") as! [String] 32 } 33 34 refresher = UIRefreshControl() 35 36 refresher?.attributedTitle = NSAttributedString(string: "Pull to refresh") 37 38 refresher?.addTarget(self, action: #selector(FirstViewController.refresh), for: UIControlEvents.valueChanged) 39 40 self.tableView.addSubview(refresher!) 41 42 refresh() 43 // Do any additional setup after loading the view, typically from a nib. 44 } 45 46 override func didReceiveMemoryWarning() { 47 super.didReceiveMemoryWarning() 48 // Dispose of any resources that can be recreated. 49 } 50 51 internal func numberOfSections(in tableView: UITableView) -> Int { 52 return 1 53 } 54 55 func refresh() { 56 tableView.reloadData() 57 refresher?.endRefreshing() 58 } 59 60 internal func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 61 if todoarray.count == todoarraydate.count { 62 return todoarray.count 63 } else { 64 return todoarraydate.count 65 } 66 } 67 68 internal func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 69 var cell = tableView.dequeueReusableCell(withIdentifier: "cell") 70 if cell == nil { 71 cell = UITableViewCell(style: .default, reuseIdentifier: "cell") 72 } 73 cell?.textLabel?.text = todoarray[indexPath.row] 74 cell?.detailTextLabel?.text = String(describing: todoarraydate[indexPath.row]) 75 return cell! 76 } 77 78 internal func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { 79 // Return false if you do not want the specified item to be editable. 80 return true 81 } 82 83 @IBAction func new(_ sender: AnyObject) { 84 self.performSegue(withIdentifier: "New To Do", sender: self) 85 } 86 87 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 88 if editingStyle == .delete { 89 todoarray.remove(at: indexPath.row) 90 todoarraydate.remove(at: indexPath.row) 91 todocontent.remove(at: indexPath.row) 92 tableView.deleteRows(at: [indexPath], with: .fade) 93 UserDefaults.standard.set(todoarray, forKey: "todoarray") 94 UserDefaults.standard.set(todoarraydate, forKey: "todoarraydate") 95 UserDefaults.standard.set(todocontent, forKey: "todocontent") 96 } 97 } 98 99 override func prepare(for segue: UIStoryboardSegue,sender: Any?){ 100 //if segue.identifier == "toSubViewController" { 101 //let SubVC: SubViewController = segue.destination as! SubViewController 102 //let indexPath: NSIndexPath = self.tableView.indexPathForSelectedRow! as NSIndexPath 103 //SubVC.a = array[indexPath] 104 //} 105 } 106 107 //画面遷移 108 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 109 print("\(indexPath)") 110 performSegue(withIdentifier: "toSubViewController", sender: nil) 111 } 112 113}
https://developer.apple.com/documentation/uikit/uitableviewdelegate/1614877-tableview こちらのメソッドを利用していますか? また、既に「swift 画面遷移 値渡し」で検索・調査した上での質問でしょうか?
そのメソッドは使っています。また検索し試していますがわかりませんでした
回答ありがとうございます。それでは、どの方法を試したのか、参考サイトのURLと自分で実装したコードを提示していただけると嬉しいです。
上記にソースを提示しました
overrideしたprepare(for:sender)メソッドの中がコメントアウトされていますね。このメソッドの中で値渡しをするのがオーソドックスな方法だと思いますが、現在このようになっている意図をお聞かせください。
正直なところ、なんとなくコメントアウトしているだけです。書いている途中でこれできるのかな?と思いこちらに投稿しました
やってみましょう!
indexPath(変数名)が渡せません。もうどうしたらいいかわかりません
breakpointをprepareメソッドの中に用意して「if segue.identifier == "toSubViewController" がtrueかどうか」「indexPathの値が正しく入っているか」「array[indexPath]の値はちゃんと入っているか」「SubVC.a に値が入っているか」を1行1行デバッグ出力して確認してみましょう!
performSegueで値渡せると思うのですが、送った値を受け取るにはどうしたら良いのですか?
どのような手法を試しましたか?toSubViewControllerには値を受け取るプロパティaが既に用意されているようですが・・・
いろいろいじったらできました!!ありがとうございます!!!!
よかったですね。この質問は解決済みにして閉じてしまいましょう!
回答1件
あなたの回答
tips
プレビュー