2016-11-02 10 views
0

photo albumZugriff auf ein Array von einer anderen Klasse

was will ich hier zu tun ist, wenn ich das erste Album zugreifen, indem Sie auf das Bild drücken i Bilder von Feuerbasis für die jeweilige Album bekommen sollte, aber ich habe ein Problem damit und Hier ist mein Code.

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 
    } 
} 
*/ 


} 

} 

Ich weiß nicht, wie das Array in der zweiten Klasse zuzugreifen, wie im Bild gezeigt, für diese Funktion überschreibt func vorbereiten (für segue

die Klasse für das zweite Array ist genau das gleiche wie die erste mit Ausnahme der 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() 
    } 
} 

Antwort

0

In der Klasse collectionVC Sie Variable haben sollte:
imageList

und in albumVC an:
prepare(for segue: UIStoryboardSegue, sender: Any?)
nach
let vc = segue.destination as! newViewController
Satz:

vc.imageList = self.post.getAllImageOfPost() 
+0

aber das hat nichts mit der Sammlung Ansicht zu tun, und wenn Sie das Bild betrachten oben i newViewController verwendet, um das Bild in vollem Umfang nutzen zu so wie kann ich die gleiche Klasse für die in der Mitte verwenden, die CollectionVC genannt wird und welche Art von Variablen ich haben sollte – Khallad

0

In solchen Fällen eine Singleton-Klasse lösen das Problem. Im Singleton-Muster erstellen Sie die Instanz Just-in-Time. Das bedeutet, wenn Sie beispielsweise ein Array setzen, wird dieses Array im Speicher reserviert, bis das Programm beendet ist.

Siehe diesen Link für die Umsetzung Singleton:

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

Verwandte Themen