代码之家  ›  专栏  ›  技术社区  ›  Suraj Bhujbal

将数据分配给集合视图后从服务器获取数据

  •  1
  • Suraj Bhujbal  · 技术社区  · 7 年前

    我不熟悉swift语言,所以不知道如何解决这个问题。在这里,我尝试使用uicollectionview显示图像。但我没有得到正确的输出,因为它在执行时不会在集合视图上显示任何内容。需要帮助的朋友。

    查看Did加载功能

     override func viewDidLoad() {
        super.viewDidLoad()
        ImageGet()
    }
    

    集合视图

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        print(defectImages.count) // returns zero value here
        return defectImages.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ImageCell", for: indexPath) as! ImageCell
        cell.image.image = defectImages[indexPath.row]
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
        let largeVC = mainStoryBoard.instantiateViewController(withIdentifier: "ImageDisplayVC") as! ImageDisplayVC
        largeVC.imgImage = defectImages[indexPath.row]
    
        self.navigationController?.pushViewController(largeVC, animated: true)
    }
    

    Alamofire获取图像

    func ImageGet() {                  
        let imageId = Int(details.id!)
        let para: Parameters = ["imageId": imageId]
    
        Alamofire.request(URL_IMG_List, method: .post, parameters: para).responseJSON { response in
            if((response.result.value) != nil) {
                let swiftyJsonVar = JSON(response.result.value!)
                if let resData = swiftyJsonVar["data"].arrayObject {
                    self.arrRes = resData as! [[String:AnyObject]]
                    for index in 0..<self.arrRes.count{
                    self.imageData.file_name = self.arrRes[index]["file_name"] as! String              
                    self.completeImagePath = self.get_image_path + self.imageData.file_name
                        self.imgpath.append(self.completeImagePath)
    
                        guard let url = URL(string: self.completeImagePath) else {return}
                        print(url)
                        if let data = try? Data(contentsOf: url) {
                            guard let image: UIImage = UIImage(data: data) else {return}
                            print(image)
                        self.defectImages.append(image as UIImage)
                        }
                    }
                    print(self.defectImages.count)
                }
            }
        }
    }
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Pankil    7 年前

    您只需重新加载 collectionView 从API获取数据后,请检查是否设置了 集合视图 dataSource delegate 从…起 storyBoard 。如果没有,请在下面的行中写入 viewDidLoad() 之前 ImageGet()

    self.collectionView.dataSource = self
    self.collectionView.delegate = self
    

    用您的代码替换以下代码。

    func ImageGet() {                  
        let imageId = Int(details.id!)
        let para: Parameters = ["imageId": imageId]
    
        Alamofire.request(URL_IMG_List, method: .post, parameters: para).responseJSON { response in
            if((response.result.value) != nil) {
                let swiftyJsonVar = JSON(response.result.value!)
                if let resData = swiftyJsonVar["data"].arrayObject {
                    self.arrRes = resData as! [[String:AnyObject]]
                    for index in 0..<self.arrRes.count{
                    self.imageData.file_name = self.arrRes[index]["file_name"] as! String              
                    self.completeImagePath = self.get_image_path + self.imageData.file_name
                        self.imgpath.append(self.completeImagePath)
    
                        guard let url = URL(string: self.completeImagePath) else {return}
                        print(url)
                        if let data = try? Data(contentsOf: url) {
                            guard let image: UIImage = UIImage(data: data) else {return}
                            print(image)
                        self.defectImages.append(image as UIImage)
                        }
                        self.collectionView.reloadData() // RELOAD COLLECTIONVIEW
                    }
                    print(self.defectImages.count)
                }
            }
        }
    }