gpt4 book ai didi

ios - 将数据分配给 Collection View 后从服务器获取数据

转载 作者:行者123 更新时间:2023-11-28 06:00:43 25 4
gpt4 key购买 nike

我是 swift 语言的新手,所以不确定如何解决这个问题。在这里,我尝试使用 uicollectionview 显示图像。但是我没有得到正确的输出,因为它在执行时没有在 Collection View 上显示任何内容。需要帮助的 friend 。

查看是否加载函数

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

Collection View

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

最佳答案

从 API 获取数据后,您只需要重新加载 collectionView 并检查您是否设置了 collectionView dataSourcestoryBoard 委托(delegate)。如果不是,则在 ImageGet() 之前的 viewDidLoad() 中写入以下行。

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

关于ios - 将数据分配给 Collection View 后从服务器获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49851031/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com