Swiftを学んでいる者です。
Navigation Controllerでリストを作成しております。
セルをタップして遷移したビューコントローラーのナビゲーションバーのタイトルに、セルのテキストを表示させたいのですが、色々と試しましたがうまくいきません。
(「1、ジャケット」のセルをタップしたら、遷移先のビューコントローラーのタイトルに「1、ジャケット」が表示される、といった感じです)
ご教授いただけますでしょうか?
お手数ですが、よろしくお願いいたします。
// TableViewController.swift import UIKit let sectionTitle = ["ジャケット","スーツ・ベスト"] let headlines0 : [(title : String, image : String)] = [(title : "1、ジャケット", image: "apron1.jpg"),(title : "2、ジャケット", image: "apron2.jpg")] let headlines1 : [(title : String, image : String)] = [(title : "1、スーツ", image: "apron3.jpg"),(title : "2、スーツ", image: "apron4.jpg")] let ishou = [headlines0,headlines1] var giveData : UIImage! class TableViewController: UITableViewController { override func viewDidLoad() { super.viewDidLoad() } // MARK: - Table view data source override func numberOfSections(in tableView: UITableView) -> Int { return sectionTitle.count } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { let sectionData = ishou[section] return sectionData.count } override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { return 40 } override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { return sectionTitle[section] } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = UITableViewCell(style: .default, reuseIdentifier: "cell") let sectionData = ishou[(indexPath as NSIndexPath).section] let cellData = sectionData[(indexPath as NSIndexPath).row] cell.textLabel?.text = cellData.title cell.imageView?.image = UIImage(named: cellData.image) cell.accessoryType = UITableViewCell.AccessoryType.disclosureIndicator return cell } override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let sectionData = ishou[(indexPath as NSIndexPath).section] let cellData = sectionData[(indexPath as NSIndexPath).row] if indexPath.section == 0 { giveData = UIImage(named: cellData.image) } else { giveData = UIImage(named: cellData.image) } performSegue(withIdentifier: "Segue", sender: nil) } override func prepare(for segue: UIStoryboardSegue, sender: Any!) { if segue.identifier == "Segue" { let vc: ViewController = (segue.destination as? ViewController)! vc.receiveData = giveData } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
import UIKit class ViewController: UIViewController { @IBOutlet weak var photo: UIImageView! var receiveData : UIImage! override func viewDidLoad() { super.viewDidLoad() photo.image = receiveData } }
回答1件
あなたの回答
tips
プレビュー