gpt4 book ai didi

swift - 如何访问我的存储并将其放置在我的收藏 View 中

转载 作者:行者123 更新时间:2023-11-30 10:45:15 24 4
gpt4 key购买 nike

我被困了几个小时,但我无法弄清楚。我对编程还是新手也许你可以帮助我

我已经创建了一个函数,可以上传 firebase Storage 中的所有内容

//this is to call the other class
var discover = MainViewController()


func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
let image = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
imageCam.image = image
MainViewController.stuffImages.append(image)
MainViewController.Stuff.append("yes")

picker.dismiss(animated: true, completion: nil)

}
/// this is how it looks in the collection view 

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return MainViewController.Stuff.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! DiscoverCollectionViewCell

cell.titleLable.text = MainViewController.Stuff[indexPath.item]
cell.imageView.image = MainViewController.stuffImages[indexPath.item]
return cell

}

如何在 firebase 中调用我的存储并将其放置在 cell.imageView.image

最佳答案

我通常会创建一个单独的数据源文件,如下所示。

class DiscoverCollectionDataSource: NSObject, UICollectionViewDataSource {

private var stuffs: [String]
private var stuffImages: [UIImage]

init(stuffs: [String], stuffImages: [UIImage]) {

self.stuffImages = stuffImages

super.init()
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.stuffImages.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! DiscoverCollectionViewCell

cell.gamerShopControllerDelegate = self.gamerShopControllerDelegate

let stuffTitle = self.stuffs[indexPath.row]
let stuffImage = self.stuffImages[indexPath.row]

cell.titleLable.text = stuffTitle
cell.imageView.image = stuffImage

return cell

}

}

之后,您可以在 Collection View 中链接数据源。

class DiscoverCollectionView: UICollectionViewController {

// Properties
var stuffs: [String]
var stuffImages: [UIImage]

lazy var dataSource: DiscoverCollectionDataSource = {
return DiscoverCollectionDataSource(stuffs: self.stuffs, stuffImages: self.stuffImages)
}()

override func viewDidLoad() {
super.viewDidLoad()

// Collection View
collectionView?.dataSource = dataSource
}

}

最后,您可以从 DiscoverCollectionView 中的 API 获取数据,也可以将数据传递到 DiscoverCollectionView 中。

希望有帮助。

关于swift - 如何访问我的存储并将其放置在我的收藏 View 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55859825/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com