商品名をrealmに登録してtableViewに登録したデータを表示し、あるデータセルを選択したら次のViewに選択したデータを表示がしたいです。
realmへのデータの登録、tableViewへの登録データの表示までは完了したのですが、問題はtableViewのセルを選択して
次の画面へ遷移したときどのように遷移前で選択したデータを取得するかがわかりません。
なんとなく考えているのはindexPath.rowで選択したセルの行番号をprepareメソッドで遷移先に渡してあげて、遷移先ではlet product = Product()
let data = product[indexPath.row]
label = product.name
というふうにする感じでしょうか?ご教授のほど宜しくおねがいします。
swift
1//モデルの作成テスト中 2class Product: Object { 3 4 @objc dynamic var name: String = "" 5 @objc dynamic var date: String = "" 6 @objc dynamic var stock: String = "" 7 8 //storeNameに属するデータ 9 let productDatas = List<ProductData>() 10 11 //プライマリキーの設定 12// override static func primaryKey() -> String? { 13// return "storeName" 14// } 15 16 17 18} 19 20class ProductData: Object { 21 22 @objc dynamic var no: String = "" 23 @objc dynamic var place: String = "" 24} 25
swift
1 //商品の登録 2 @IBAction func tappedRegistration(_ sender: Any) { 3 4 5 if productName.text == "" { 6 7 let alertController = UIAlertController(title: "警告", message: "商品名を入力してください", preferredStyle: .alert) 8 let alertAction = UIAlertAction(title: "了解", style: .default) { (UIAlertAction) in 9 print("あ") 10 } 11 alertController.addAction(alertAction) 12 present(alertController, animated: true, completion: nil) 13 14 } else { 15 //モデルのインスタンス化 16 17 let product = Product() 18 product.name = productName.text! 19 product.date = dateTextField.text! 20 product.stock = stock.text! 21 try! realm.write { 22 realm.add(product) 23 }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/20 08:06
2018/11/20 08:33
2018/11/21 03:56