2017-08-24 3 views
-2

J'ai essayé de faire un projet qui devrait contenir collectionView qui nagivates à un autre collectionView après avoir sélectionné un élément de celui-ci.comment naviguer à partir d'une vue de la collection après avoir sélectionné didselectitem à une autre vue de la collection

import UIKit 

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate { 
@IBOutlet weak var myCollectionView: UICollectionView! 
var imgURLArray = [String]() 

var nameArray = [String]() 
final let urlString = "https://unembittered-vector.000webhostapp.com/JSON/alljson.txt" 
override func viewDidLoad() { 
    super.viewDidLoad() 
    self.downloadJsonWithURL() 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 

} 
func downloadJsonWithURL() { 
    let url = NSURL(string: urlString) 
    URLSession.shared.dataTask(with: (url as? URL)!, completionHandler: {(data, response, error) -> Void in 

     if let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary { 
      print(jsonObj!.value(forKey: "Materials")!) 

      if let actorArray = jsonObj!.value(forKey: "Materials") as? NSArray { 
       for actor in actorArray{ 
        if let actorDict = actor as? NSDictionary { 
         if let name = actorDict.value(forKey: "name") { 
          self.nameArray.append(name as! String) 
         }/* 
         if let name = actorDict.value(forKey: "dob") { 
          self.dobArray.append(name as! String) 
         } */ 
         if let name = actorDict.value(forKey: "image") { 
          self.imgURLArray.append(name as! String) 
         } 

        } 
       } 
      } 


      OperationQueue.main.addOperation({ 
       self.myCollectionView.reloadData() 
      }) 
     } 
    }).resume() 
} 


func downloadJsonWithTask() { 

    let url = NSURL(string: urlString) 

    var downloadTask = URLRequest(url: (url as? URL)!, cachePolicy: URLRequest.CachePolicy.reloadIgnoringCacheData, timeoutInterval: 20) 

    downloadTask.httpMethod = "GET" 

    URLSession.shared.dataTask(with: downloadTask, completionHandler: {(data, response, error) -> Void in 

     let jsonData = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) 

     print(jsonData!) 

    }).resume() 
} 


//Number of views 
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 

    return imgURLArray.count 

} 

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

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! myCell 
    print("test") 
    cell.nameLabel.text = nameArray[indexPath.row] 
    let imgURL = NSURL(string: imgURLArray[indexPath.row]) 

    if imgURL != nil { 
     print("check") 
     let data = NSData(contentsOf: (imgURL as? URL)!) 
     //cell.myImageView.image = UIImage(data: data as! Data) 
     cell.myImageView.image = UIImage(data: data as! Data) 

    } 
    else{ 
     print("error")} 
    //cell.myImageView.image = UIImage(named: array[indexPath.row] + ".JPG") 
    return cell 
} 

/*func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
    let vc = self.storyboard?.instantiateViewController(withIdentifier: "ViewController") as! ViewController 
    //vc.imageString = imgURLArray[indexPath.row] 
    //vc.nameString = nameArray[indexPath.row] 
    //vc.dobString = dobArray[indexPath.row] 

    //cell.myImageView.image = UIImage(named: array[indexPath.row] + ".JPG") 
    //return cell1 

    //self.navigationController?.pushViewController(vc, animated: true)  
}*/ 
} 
+0

Quel est le problème auquel vous êtes confronté? – PGDev

+0

je suis nouveau chez ios .. je veux aller à une autre vue de la collection après avoir cliqué sur l'élément à la vue de la collection que j'ai fait dans le code ci-dessus. –

+0

S'il vous plaît dites-nous ce que vous voulez exactement. –

Répondre

0

Voici un exemple sur la façon dont vous pouvez mettre en œuvre ce que vous cherchez:

1. Storyboard

enter image description here

2. Voir Contrôleurs

import UIKit 

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate 
{ 
    override func viewDidLoad() 
    { 
     super.viewDidLoad() 
    } 

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

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 
    { 
     return collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) 
    } 

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) 
    { 
     let controller = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController 
     self.navigationController?.pushViewController(controller, animated: true) 
    } 
} 

class SecondViewController: UIViewController, UICollectionViewDataSource 
{ 
    override func viewDidLoad() 
    { 
     super.viewDidLoad() 
    } 

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

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 
    { 
     return collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) 
    } 
} 

Répondre si vous rencontrez un problème.

+0

merci, j'apprécie pour votre réponse –

+0

Avez-vous réalisé ce que vous avez exigé? – PGDev

+0

Pouvez-vous m'aider avec un exemple de vue de collection qui charge des données de json avec l'image et clique sur un élément de la vue pour naviguer vers une autre page avec vue de collection qui charge à nouveau les données de json. @PGDev –