実現したいこと
課題リストを作っています
セルのタップで画面遷移をして詳細の情報を表示したいのですが
画面遷移の時にIndexPathを値渡しをできないのでしょうか?
課題名、課題期日は配列で管理しています
必要な場合はソースを提示します
質問があればお願いします
簡単ですがよろしくお願いします
参考にしたサイトはブックマーク等していなかったためわかりません。
すみません
import UIKit
class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet var tableView: UITableView!
var todoarray = [String]()
var todoarraydate = [String]()
var todocontent = [String]()
var refresher: UIRefreshControl?
override func viewDidLoad() {
super.viewDidLoad()
if UserDefaults.standard.array(forKey: "todoarray") == nil {
todoarray = [String]()
UserDefaults.standard.set(todoarray, forKey: "todoarray")
} else {
todoarray = UserDefaults.standard.array(forKey: "todoarray") as! [String]
}
if UserDefaults.standard.array(forKey: "todoarraydate") == nil {
todoarraydate = [String]()
UserDefaults.standard.set(todoarraydate, forKey: "todoarraydate")
} else {
todoarraydate = UserDefaults.standard.array(forKey: "todoarraydate") as! [String]
}
if UserDefaults.standard.array(forKey: "todocontent") == nil {
todocontent = [String]()
UserDefaults.standard.set(todocontent, forKey: "todocontent")
} else {
todocontent = UserDefaults.standard.array(forKey: "todocontent") as! [String]
}
refresher = UIRefreshControl()
refresher?.attributedTitle = NSAttributedString(string: "Pull to refresh")
refresher?.addTarget(self, action: #selector(FirstViewController.refresh), for: UIControlEvents.valueChanged)
self.tableView.addSubview(refresher!)
refresh()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
internal func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func refresh() {
tableView.reloadData()
refresher?.endRefreshing()
}
internal func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if todoarray.count == todoarraydate.count {
return todoarray.count
} else {
return todoarraydate.count
}
}
internal func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
if cell == nil {
cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
}
cell?.textLabel?.text = todoarray[indexPath.row]
cell?.detailTextLabel?.text = String(describing: todoarraydate[indexPath.row])
return cell!
}
internal func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return true
}
@IBAction func new(_ sender: AnyObject) {
self.performSegue(withIdentifier: "New To Do", sender: self)
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
todoarray.remove(at: indexPath.row)
todoarraydate.remove(at: indexPath.row)
todocontent.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
UserDefaults.standard.set(todoarray, forKey: "todoarray")
UserDefaults.standard.set(todoarraydate, forKey: "todoarraydate")
UserDefaults.standard.set(todocontent, forKey: "todocontent")
}
}
override func prepare(for segue: UIStoryboardSegue,sender: Any?){
//if segue.identifier == "toSubViewController" {
//let SubVC: SubViewController = segue.destination as! SubViewController
//let indexPath: NSIndexPath = self.tableView.indexPathForSelectedRow! as NSIndexPath
//SubVC.a = array[indexPath]
//}
}
//画面遷移
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("\(indexPath)")
performSegue(withIdentifier: "toSubViewController", sender: nil)
}
}
-
気になる質問をクリップする
クリップした質問は、後からいつでもマイページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
クリップを取り消します
-
良い質問の評価を上げる
以下のような質問は評価を上げましょう
- 質問内容が明確
- 自分も答えを知りたい
- 質問者以外のユーザにも役立つ
評価が高い質問は、TOPページの「注目」タブのフィードに表示されやすくなります。
質問の評価を上げたことを取り消します
-
評価を下げられる数の上限に達しました
評価を下げることができません
- 1日5回まで評価を下げられます
- 1日に1ユーザに対して2回まで評価を下げられます
質問の評価を下げる
teratailでは下記のような質問を「具体的に困っていることがない質問」、「サイトポリシーに違反する質問」と定義し、推奨していません。
- プログラミングに関係のない質問
- やってほしいことだけを記載した丸投げの質問
- 問題・課題が含まれていない質問
- 意図的に内容が抹消された質問
- 過去に投稿した質問と同じ内容の質問
- 広告と受け取られるような投稿
評価が下がると、TOPページの「アクティブ」「注目」タブのフィードに表示されにくくなります。
質問の評価を下げたことを取り消します
この機能は開放されていません
評価を下げる条件を満たしてません
質問の評価を下げる機能の利用条件
この機能を利用するためには、以下の事項を行う必要があります。
- 質問回答など一定の行動
-
メールアドレスの認証
メールアドレスの認証
-
質問評価に関するヘルプページの閲覧
質問評価に関するヘルプページの閲覧
15分調べてもわからないことは、teratailで質問しよう!
- ただいまの回答率 88.23%
- 質問をまとめることで、思考を整理して素早く解決
- テンプレート機能で、簡単に質問をまとめられる
質問への追記・修正、ベストアンサー選択の依頼
idonotknow
2017/09/17 12:35
https://developer.apple.com/documentation/uikit/uitableviewdelegate/1614877-tableview こちらのメソッドを利用していますか? また、既に「swift 画面遷移 値渡し」で検索・調査した上での質問でしょうか?
ssh_u
2017/09/17 13:17
そのメソッドは使っています。また検索し試していますがわかりませんでした
idonotknow
2017/09/17 13:19
回答ありがとうございます。それでは、どの方法を試したのか、参考サイトのURLと自分で実装したコードを提示していただけると嬉しいです。
ssh_u
2017/09/17 13:24
上記にソースを提示しました
idonotknow
2017/09/17 13:35
overrideしたprepare(for:sender)メソッドの中がコメントアウトされていますね。このメソッドの中で値渡しをするのがオーソドックスな方法だと思いますが、現在このようになっている意図をお聞かせください。
ssh_u
2017/09/17 13:43
正直なところ、なんとなくコメントアウトしているだけです。書いている途中でこれできるのかな?と思いこちらに投稿しました
idonotknow
2017/09/17 13:45
やってみましょう!
ssh_u
2017/09/17 13:52
indexPath(変数名)が渡せません。もうどうしたらいいかわかりません
idonotknow
2017/09/17 13:59
breakpointをprepareメソッドの中に用意して「if segue.identifier == "toSubViewController" がtrueかどうか」「indexPathの値が正しく入っているか」「array[indexPath]の値はちゃんと入っているか」「SubVC.a に値が入っているか」を1行1行デバッグ出力して確認してみましょう!
ssh_u
2017/09/17 14:16
performSegueで値渡せると思うのですが、送った値を受け取るにはどうしたら良いのですか?
idonotknow
2017/09/17 14:19
どのような手法を試しましたか?toSubViewControllerには値を受け取るプロパティaが既に用意されているようですが・・・
ssh_u
2017/09/17 15:00
いろいろいじったらできました!!ありがとうございます!!!!
idonotknow
2017/09/17 15:21
よかったですね。この質問は解決済みにして閉じてしまいましょう!