viewcontroller - 将数组数据从 CollectionView 传递到下一个 ViewController
问题描述
我正在开发 IOS 应用程序,我想将食物图像和标题传递给下一个 ViewController。食物图像和标题从 API 中检索并存储在数组中,并使用 SDWebImage 显示食物图像和标题。食物图像和标题使用 public var foodTitle = String 和 public var foodImage = String。
是否可以将单击的图像数据从集合视图单元传递到下一个 ViewController?
我尝试使用下面的代码,但仍然无法在 ViewController 上显示图像和标题。
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath){
let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let placeOrder = mainStoryboard.instantiateViewController(withIdentifier: "PlaceOrderViewController") as! PlaceOrderViewController
placeOrder.name = self.foodTitle[indexPath.item]
placeOrder.image = self.foodImage[indexPath.row]
self.navigationController?.pushViewController(placeOrder, animated: true)
}
下面是集合视图的代码:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! MainPageCollectionViewCell
cell.FoodTitle.text = self.foodTitle[indexPath.item]
cell.Food.sd_setImage(with: URL(string: foodImage[indexPath.row]),placeholderImage: UIImage(named: "image"))
return cell
}
解决方案
当您传递图像 URL 和标题时,这是可能的。在下一个 viewController 中创建一个图像视图,并通过 SDWebImage 异步调用加载图像视图的图像。
推荐阅读
- javascript - 使用 Javascript 添加公式时解析 #Error
- c# - OAuth 实现 2.0 使用 .Net Core , Swagger
- javascript - 如何从父窗口更改 iframe 上的域?
- node.js - 如何为 Express 中间件在内存中缓存 .jpg?
- html - 为什么移动设备上的 Safari 需要这么长时间才能从内存中加载图像?
- java - 使用基于用户登录的缓存键
- javascript - 功能:错误 stackTraceLimit:10 - JavaScript、Google AppScript
- c# - 我读到你不能离开 Teams 去处理核心和非核心场景
- laravel - 如何使用 Laravel 和 Tailwind CSS 创建下拉导航
- git - 将多个 Git 差异显示为单个统一差异