【问题标题】:access an array from another class从另一个类访问数组
【发布时间】:2016-11-02 21:57:41
【问题描述】:

我在这里想要做的是,当我通过按图片访问第一张专辑时,我应该从 firebase 获取该特定专辑的图片,但我有一个问题,这是我的代码。

class albumsVC: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource{


    @IBOutlet weak var collectionView: UICollectionView!

    var posts = [Post]()
    static var imageCache: NSCache<NSString, UIImage> = NSCache()


    override func viewDidLoad() {
    super.viewDidLoad()


    collectionView.delegate = self
    collectionView.dataSource = self


    DataService.ds.REF_POSTS3.observe(.value, with: { (snapshot) in

        if let snapshot = snapshot.children.allObjects as? [FIRDataSnapshot] {

            for snap in snapshot {
                print ("SNAP: \(snap)")

                if let postDict = snap.value as? Dictionary<String, AnyObject>{
                    let key = snap.key
                    let post = Post(postKey: key , postData: postDict)
                    self.posts.append(post)
                }
            }
        }
        self.collectionView.reloadData()

    })

    }


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

    func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
    }

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


    let post = posts[indexPath.row]

    if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)as? collectionViewCellAlbums {

        if let img = albumsVC.imageCache.object(forKey: post.mainImg as NSString) {
            cell.configureCell(post: post, img: img)
            return cell

        }else {

            cell.configureCell(post: post)
            return cell
        }
    }
    else {
        return collectionViewCellAlbums()
    }


    }


    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    self.performSegue(withIdentifier: "showAlbum", sender: self)
    }



    override func prepare(for segue: UIStoryboardSegue, sender: Any?)
    {

    /**
    if segue.identifier == "showAlbum"
    {

        let indexPaths = self.collectionView!.indexPathsForSelectedItems!
        let indexPath = indexPaths[0] as IndexPath
        let vc = segue.destination as! newViewController
        let post = posts[indexPath.row]
        if let img = CollectionVC.imageCache.object(forKey: post.imageUrl as NSString) {
            vc.image = img
    }
}
*/


}

}

我不知道如何访问第二类中的数组如图所示这个函数覆盖func prepare(for segue

除了newViewController.swift之外,第二个数组的类与第一个完全相同

class newViewController: UIViewController {

    @IBOutlet weak var imageView: UIImageView!

       var image = UIImage()


    override func viewDidLoad() {
    super.viewDidLoad()

       self.imageView.image = self.image

    }

    override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    }
}

【问题讨论】:

    标签: firebase uicollectionview firebase-realtime-database swift3


    【解决方案1】:

    collectionVC 类中,您应该有变量:
    imageList

    albumVC 中:
    prepare(for segue: UIStoryboardSegue, sender: Any?)
    之后
    let vc = segue.destination as! newViewController
    设置:

    vc.imageList = self.post.getAllImageOfPost()
    

    【讨论】:

    • 但这与集合视图无关,如果你看上面的图片,我使用 newViewController 全屏查看图像,那么我如何为中间的那个使用相同的类这称为 CollectionVC 以及我应该拥有什么类型的变量
    【解决方案2】:

    在这种情况下,使用单例类可以解决问题。在单例模式中,您可以即时创建实例。这意味着当你设置一个数组时,这个数组将被分配到内存中,直到程序完成运行。

    单例实现参考此链接:

    http://www.galloway.me.uk/tutorials/singleton-classes/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-02
      • 1970-01-01
      • 2012-05-03
      • 2012-09-07
      • 2014-04-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多