将数组数据从CollectionView传递到下一个ViewController

问题描述 投票:0回答:1

我正在开发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
}
viewcontroller collectionview swift4.2
1个回答
0
投票

当您传递图像URL和标题时,可能的。在下一个viewController中创建图像视图,并通过SDWebImage异步调用加载图像视图的图像。

© www.soinside.com 2019 - 2024. All rights reserved.