我想的CollectionView使用JSON解析加載圖像。我收到了一系列圖片網址,但圖片未在UIImageView
上顯示。在獲取JSON數組並在UICollectionView
的UIImageView
中顯示它之後,我使用了此代碼。填充的CollectionView從JSON圖像URL快捷
if let url = NSURL(string: self.Products[indexPath.row].image) {
if let data = NSData(contentsOfURL: url){
cell.product_image.contentMode = UIViewContentMode.ScaleAspectFit
cell.product_image.image = UIImage(data: data)
}
}
但我無法加載圖像,但文本顯示。我已經在cellForItemAtIndexPath
方法中使用此代碼..任何人都可以建議我,我做錯了什麼?
func jsonParsingFromURL(){
// 1
let reposURL = NSURL(string: "http://api.room2shop.com/api/product/GetProducts?categoryId=24&filter=2&pageNumber=1")
// 2
if let JSONData = NSData(contentsOfURL: reposURL!) {
// 3
do
{
if let json = try NSJSONSerialization.JSONObjectWithData(JSONData, options: .AllowFragments) as? NSDictionary {
// 4
if let reposArray = json["ProductList"] as? [NSDictionary] {
// 5
for item in reposArray {
Products.append(ProductList(json: item))
}
}
}
}
catch {
print("Error with Json: \(error)")
}
}
}
這是我的JSON解析代碼
是你曾經做了'self.collectionView?.reloadData()'??? – DeyaEldeen
不..我不是那樣 –