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

标签 swift firebase uicollectionview

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

我已经创建了一个函数,可以上传 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/

相关文章:

ios - firebaseui-ios FirebaseTableViewDataSource 与 UITableView 同步

ios - minimumInteritemSpacingForSectionAtIndex 和 minimumLineSpacingForSectionAtIndex 有什么区别

ios - 为什么在设备旋转的纵向模式下不专门调用 UICollectionViewDelegateFlowLayout 方法?

Swift 5 CollectionView 通过长按单元格获取 indexPath

ios - FileManager 更改不适用于重新排序并且具有 ErrorDomain Code=17 "File exists"

ios - 无法使用 Firebase 存储 UI 加载某些图像(SD Web 图像)

javascript - Firebase:手动删除一条路径时如何更新数组长度?

ios - 即使将planeDetection设置为空,ARKitplaneDetection仍会继续

swift - applicationShouldTerminate 在 macOS Swift 应用程序中意外调用

ios - 在单元测试中处理 MainThread 执行