代码之家  ›  专栏  ›  技术社区  ›  user8303866

如何在Swift中显示集合视图上的图像阵列

  •  2
  • user8303866  · 技术社区  · 7 年前
    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()
    
                    }
                }
    
            }
        })
    }
    

    如何使用键显示图像阵列 gallery_images print("Gallery Images Key: \(gallerySingle)") 在控制台上打印所需的图像阵列,但我无法在上显示 collectionView JSON 响应为 http://www.jsoneditoronline.org/?id=8eccb63976d76be7d0b2d1b0e8f02306 .我正在使用 SDWebImage 在我的项目也。我已经查过了 datasource 连接(&A); 集合视图 IBoutlet以及牢房内的图像。我是Swift的初学者,请帮助,如果需要任何进一步的信息,请询问

    2 回复  |  直到 7 年前
        1
  •  1
  •   user8303866 user8303866    7 年前

    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)
                    }
                }
            }
        })
    }
    
        2
  •  0
  •   Jaydeep Vora    7 年前

    Swift 3.0

    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)
                    }
    
                }
            })