Tableviewcell 数据不会转换到另一个视图控制器
Posted
技术标签:
【中文标题】Tableviewcell 数据不会转换到另一个视图控制器【英文标题】:Tableviewcell data won't translate to another view controller 【发布时间】:2022-01-07 16:30:09 【问题描述】:我在我的listingshopvc中做了一个解析json,它显示了。现在我想选择一行,它会弹出另一个视图控制器,但有错误只是一个没有错误的空白页。我只想按一行并将其转换为显示在另一个视图控制器上。
我在以下视图控制器中有列表:ListingShopVc,ProductDetail,TableCellData
ListingShopVC
import UIKit
import CoreData
class ListingShopVC: UIViewController, UITableViewDelegate, UITableViewDataSource
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var creditsdisplay: UILabel!
var context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
var myUser:[User] = []
var mySecond:[Product] = []
var id:String = ""
var name:String = ""
var price:Double = 0.0
var image:String = ""
var details:String = ""
override func viewDidLoad()
super.viewDidLoad()
fetch()
tableView.delegate = self
tableView.dataSource = self
extracted()
creditsdisplay.text = "You have 200 credits"
// MARK: - Table view data source
func numberOfSections(in tableView: UITableView) -> Int
// #warning Incomplete implementation, return the number of sections
return 1
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
// #warning Incomplete implementation, return the number of rows
return mySecond.count
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
let cell = tableView.dequeueReusableCell(withIdentifier: "hello", for: indexPath) as! TableCellData
// Configure the cell...
cell.shopTitle.text = mySecond[indexPath.row].name
cell.shopPrice.text = "$" + String(mySecond[indexPath.row].price) + "0"
cell.shopDesc.text = mySecond[indexPath.row].description
if let url = URL(string: mySecond[indexPath.row].image)
DispatchQueue.global().async
let data = try? Data(contentsOf: url)
if let data = data
let image = UIImage(data: data)
DispatchQueue.main.async
cell.shopImageView.image = image
return cell
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
id = mySecond[indexPath.row].id
name = mySecond[indexPath.row].name
price = mySecond[indexPath.row].price
details = mySecond[indexPath.row].description
image = mySecond[indexPath.row].image
performSegue(withIdentifier: "toDetails", sender: self)
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
if segue.identifier == "toDetails"
let vc = segue.destination as! ProductDetail
vc.productPicture = image
vc.productName = name
vc.productPrice = price
vc.productDetails = details
vc.productID = id
print(vc.productName)
func extracted()
guard let url = URL(string: "http://rajeshrmohan.com/sport.json")
else return
let task = URLSession.shared.dataTask(with: url)
(data,response,error) in
guard let dataResponse = data,
error == nil else
print(error?.localizedDescription ?? "Response Error")
return
do
let decoder = JSONDecoder()
let model:[Product] = try decoder.decode([Product].self, from: dataResponse)
//print(model)
for i in 0..<model.count
self.mySecond.append(model[i])
DispatchQueue.main.async
self.tableView.reloadData()
catch let parsingError
print("Error", parsingError)
task.resume()
func fetch()
userList = try! context.fetch(User.fetchRequest())
tableView.reloadData()
TableViewCell
import UIKit
class TableCellData: UITableViewCell
@IBOutlet weak var shopImageView: UIImageView!
@IBOutlet weak var shopTitle: UILabel!
@IBOutlet weak var shopPrice: UILabel!
@IBOutlet weak var shopDesc: UILabel!
override func awakeFromNib()
super.awakeFromNib()
// Initialization code
override func setSelected(_ selected: Bool, animated: Bool)
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
产品详情
import UIKit
import CoreData
class ProductDetail: UIViewController
var context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
var myArray:[Cart] = []
var mySecondArray:[Products] = []
var newTots:Double = 0.0
@IBOutlet weak var AddImage: UIImageView!
@IBOutlet weak var AddTitle: UILabel!
@IBOutlet weak var AddPrice: UILabel!
@IBOutlet weak var AddDesc: UILabel!
var productID = String()
var productName = String()
var productPrice:Double = 0.0
var productPicture = String()
var productDetails:String = ""
override func viewDidLoad()
super.viewDidLoad()
AddTitle.text = productName
AddDesc.text = productDetails
AddPrice.text = String(productPrice)
if let imageUrl = URL(string: productPicture),
let imageData = try? Data(contentsOf: imageUrl)
AddImage.image = UIImage(data: imageData)
@IBAction func add2Cart(_ sender: Any)
let neww = Cart(context: context)
neww.image = productPicture
neww.name = productName
neww.desc = productDetails
neww.price = productPrice
neww.total = productPrice
try! context.save()
performSegue(withIdentifier: "gotoCheckout", sender: nil)
func fetch()
self.mySecondArray = try! context.fetch(Products.fetchRequest())
它显示了什么 [1]:https://i.stack.imgur.com/XWUt0.png
【问题讨论】:
你有没有调试过ProductDetail的viewDidLoad,并在调用viewDidLoad的时候检查产品信息是否可用?我最好的猜测是它不是..尝试将代码放在 viewWillAppear.. 尝试将下面的代码移动到 viewWillApppear 而不是 viewDidAppear AddTitle.text = productName AddDesc.text = productDetails AddPrice.text = String(productPrice) if let imageUrl = URL(string: productPicture), let imageData = try? Data(contentsOf: imageUrl) AddImage.image = UIImage(data: imageData) 同样的问题。我已经尝试了 viewwillappear 和 viewdidappear 以防万一和相同的输出。只是一个没有错误的空白页 【参考方案1】:-> 你只需要调用表格视图didSelectRowAt的委托方法
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
let tag = projectEquipmentList?[indexPath.row].tag // Object data to take in next VC
let QR = R.storyboard.main.qrScannerVC()! // View Controller
QR.manageVC(projectEquipmentTag: tag!) // setter method of View controller to set data
navigationController?.pushViewController(QR, animated: true) // push to next view controller
【讨论】:
以上是关于Tableviewcell 数据不会转换到另一个视图控制器的主要内容,如果未能解决你的问题,请参考以下文章
将数据从 tableviewcell 传递到另一个 viewcontroller
单击tableview标题按钮时如何将tableviewcell内的collectionview中的数据显示到另一个视图控制器
我正在尝试将 tableviewcell 从一个表添加到另一个表 Xcode