UITableViewCell数据无法传递到其他ViewController导致空白页问题求助
问题排查与修复方案
按优先级依次排查修复即可解决详情页空白问题:
- 第一优先级检查IBOutlet与类绑定
确认Storyboard/Xib中详情页的两个配置:- 详情页ViewController的Custom Class已正确设置为
ProductDetail AddImage、AddTitle、AddPrice、AddDesc四个IBOutlet已和界面上的对应控件正确关联,关联错误时控件为nil,赋值无效果也不会抛出报错
- 详情页ViewController的Custom Class已正确设置为
- 修复详情页图片同步加载问题
当前详情页在主线程同步加载网络图片,会阻塞UI甚至加载失败,替换ProductDetail的viewDidLoad代码:override func viewDidLoad() { super.viewDidLoad() // 可先加打印确认值是否传递成功 print("接收到商品名:\(productName) 价格:\(productPrice)") AddTitle.text = productName AddDesc.text = productDetails AddPrice.text = "$" + String(productPrice) + "0" // 改成异步加载图片和列表页逻辑一致 if let url = URL(string: productPicture){ DispatchQueue.global().async { let data = try? Data(contentsOf: url) if let data = data, let image = UIImage(data: data) { DispatchQueue.main.async { self.AddImage.image = image } } } } } - 优化传值逻辑避免取值错误
当前用全局变量存储选中商品数据容易出现并发/多次点击取值错误,优化ListingShopVC的选中与传值逻辑:- 删掉
ListingShopVC中声明的id、name、price、image、details五个全局变量 - 替换
didSelectRowAt与prepare方法:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { // 直接把选中的indexPath作为参数传递 performSegue(withIdentifier: "toDetails", sender: indexPath) } override func prepare(for segue: UIStoryboardSegue, sender: Any?) { guard segue.identifier == "toDetails", let indexPath = sender as? IndexPath, let vc = segue.destination as? ProductDetail else { return } let targetProduct = mySecond[indexPath.row] vc.productID = targetProduct.id vc.productName = targetProduct.name vc.productPrice = targetProduct.price vc.productDetails = targetProduct.description vc.productPicture = targetProduct.image } - 删掉
- 确认segue标识正确性
检查Storyboard中跳转详情页的segue标识拼写完全为toDetails,大小写完全匹配,标识错误会导致传值逻辑不执行。
内容的提问来源于stack exchange,提问作者KAKEN
相关产品推荐
相关产品推荐

