【问题标题】:Pass array data from CollectionView to next ViewController将数组数据从 CollectionView 传递到下一个 ViewController
【发布时间】:2020-03-01 08:12:59
【问题描述】:

我正在开发 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)
}

下面是collection view的代码:

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 传递给 PlaceOrderViewController。

标签: viewcontroller collectionview swift4.2


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2016-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-04
    • 1970-01-01
    • 1970-01-01
    • 2018-09-26
    相关资源
    最近更新 更多