1
にコレクションビューに
func numberOfSections(in collectionView: UICollectionView) -> Int { 
    return 1 
} 

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    print("gallery count : \(self.arrayGallerySingle.count)") 
    return self.arrayGallerySingle.count 
} 

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "gallerycell", for: indexPath) as! GalleryViewCell 
return cell 
} 

//API Fetching method 
func galleryFetch(){ 

    let prs = [ 
     "id": dealIDGallery, 
     "deal_single_page": "1" as String 
    ] 

    Service.StartWithoutLoading(prs as [String : AnyObject]?, onCompletion: { result in 
     let jsonResponseSingle = result as? NSDictionary 
     print(" jsonResponse\(String(describing: jsonResponseSingle))") 

     if let resultGallery = jsonResponseSingle?.value(forKey: "result") as? NSArray{ 

      for keyValuesGallery in resultGallery { 

       let gallerySingle = (keyValuesGallery as AnyObject).value(forKey: "gallery_images") as? NSArray 

       print("Gallery Images Key: \(gallerySingle)") 
       self.arrayGallerySingle = gallerySingle as! [AnyObject] 

       DispatchQueue.main.async {() -> Void in 
        self.collectionView?.reloadData() 

       } 
      } 

     } 
    }) 
} 

を画像の配列を表示する方法をどのように私はUICollectionViewCellのキーgallery_imagesで画像の配列が表示されます?この行print("Gallery Images Key: \(gallerySingle)")は、コンソール上の画像の必要な配列を出力しますが、私はcollectionViewに表示することができません。私のJSONの応答はhttp://www.jsoneditoronline.org/?id=8eccb63976d76be7d0b2d1b0e8f02306です。私もSDWebImageを私のプロジェクトに使っています。すでにdatasource接続& collectionView IBoutletとセル内の画像をチェックしています。私はスウィフトPLSのヘルプで初心者午前、それ以上の情報を必要に応じてplsははスウィフト

答えて

0

スウィフト3.0

あなたはcollectionView細胞画像で画像をロードするSDWebcacheと、このように実装することができ尋ねます。

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "gallerycell", for: indexPath) as! GalleryViewCell 

      if let imgUrl = arrayGallerySingle[indexPath.row] as? String { 
       if let url = URL(string: imgUrl) { 
        //Replace with your imageView outlet 
        cell.imageView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "place holder image"), options: .lowPriority) 
       } 
      } 
      return cell 
     } 

そして、あなたはcollectionViewで6セルを表示したいなら、あなたは以下のように、セルの定義されたサイズにUICollectionViewLayout方法を設定する必要があります。

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize 
    { 
    return CGSize(width: collectionView.frame.size.width/2, height: collectionView.frame.size.height/3) 

} 

配列数が空でない場合や、ループ内でコレクションビューを再読み込みする理由は以下のように実装できます。

Service.StartWithoutLoading(prs as [String : AnyObject]?, onCompletion: { result in 
      let jsonResponseSingle = result as? NSDictionary 
      print(" jsonResponse\(String(describing: jsonResponseSingle))") 

      if let resultGallery = jsonResponseSingle?.value(forKey: "result") as? NSArray{ 

       for keyValuesGallery in resultGallery { 

        let gallerySingle = (keyValuesGallery as AnyObject).value(forKey: "gallery_images") as? NSArray 

        print("Gallery Images Key: \(gallerySingle)") 
        self.arrayGallerySingle = gallerySingle as! [AnyObject] 
       } 

       if self.arrayGallerySingle.count > 0 { 

        DispatchQueue.main.async {() -> Void in 
         self.collectionView?.reloadData() 

        } 

       } else { 
        let alertController = UIAlertController(title: "Your Title", message: "their is no images", preferredStyle: .alert) 
        alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil)) 
        self.present(alertController, animated: tru, completion: nil) 
       } 

      } 
     }) 
+0

を助けることを願っています@Jaydeep –

+0

間隔、それは私の問題を解決し、私は電池なしでコレクションビューで一度に6枚の画像を表示したいI私も画像がない彼らの@Jaydeepです警告を表示したい –

+0

@サニーは更新された答えを確認します。 – Jaydeep

0

Swift 3.0これは私の更新の答えです、それはまた誰か

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

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    print("gallery count : \(self.arrayGallerySingle.count)") 
    return self.arrayGallerySingle.count 

} 

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "gallerycell", for: indexPath) as! GalleryViewCell 
    if let imgUrl = arrayGallerySingle[indexPath.row] as? String { 
     if let url = NSURL(string: imgUrl) { 
      cell.galleryImage?.sd_setImage(with: url as URL, placeholderImage: UIImage(named: "place holder image"), options: .lowPriority) 
     } 
     else{ 
      let alertController = UIAlertController(title: "No Gallery Images", message: "test", preferredStyle: .alert) 
      let okButton = UIAlertAction(title: "Okay", style: .default, handler: nil) 
      alertController.addAction(okButton) 
      alertController.present(alertController, animated: true, completion: nil) 
     } 
    } 
return cell 
} 

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { 
    return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) 
} 

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat { 
    return 0.0 
} 

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { 
    return 0.0 
} 

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize 
{ 
    return CGSize(width: collectionView.frame.size.width/2, height: collectionView.frame.size.height/3) 

} 

func galleryFetch(){ 

    let prs = [ 
     "id": dealIDGallery, 
     "deal_single_page": "1" as String 
    ] 

    Service.StartWithoutLoading(prs as [String : AnyObject]?, onCompletion: { result in 
     let jsonResponseSingle = result as? NSDictionary 
     print(" jsonResponse\(String(describing: jsonResponseSingle))") 

     if let resultGallery = jsonResponseSingle?.value(forKey: "result") as? NSArray{ 

      for keyValuesGallery in resultGallery { 

       let gallerySingle = (keyValuesGallery as AnyObject).value(forKey: "gallery_images") as? NSArray 

       print("Gallery Images Key: \(gallerySingle)") 
       self.arrayGallerySingle = gallerySingle as! [AnyObject] 

       if self.arrayGallerySingle.count > 0 { 

        DispatchQueue.main.async {() -> Void in 
         self.collectionView?.reloadData() 

        } 
       } else { 
        let alertController = UIAlertController(title: "Message", message: "No images found.", preferredStyle: .alert) 
        alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: self.doSomething)) 
        self.present(alertController, animated: true, completion: nil) 
       } 
      } 
     } 
    }) 
}