从另一个类访问一个数组

时间:2016-11-02 03:33:29

标签: firebase uicollectionview firebase-realtime-database swift3

photo album

我想在这里做的是当我通过按下图片访问第一张专辑时,我应该从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()
    }
}

2 个答案:

答案 0 :(得分:0)

在班级collectionVC中你应该有变量:
 imageList

并在albumVC at:
prepare(for segue: UIStoryboardSegue, sender: Any?)

let vc = segue.destination as! newViewController
设置:

vc.imageList = self.post.getAllImageOfPost()

答案 1 :(得分:0)

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

有关Singleton实现的信息,请参阅此链接:

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