您好,我正在開發一個應用程序使用swift 2.2 am從服務器獲取數據獲取多個圖像,這是由逗號分隔例如..,如何傳遞多個圖像,用逗號分隔收集視圖使用swift
{
"success": 1,
"error": 0,
"message": [{
"title": "aver monitor",
"image": "3037-1486200291.jpg,5056_25326756.jpg",
"views": "0",
"price": "12000",
"postdate": "2017-02-04 14:54:51",
"type": "0"
}]
}
在圖像值獲取圖像我需要將該圖像傳遞到集合視圖。
圖像是來自不超過超過5服務器獲取,我想只顯示5,所以我寫的功能爲:
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
我可以能夠通過使用逗號單獨的值分隔通過字符串(「,」)如果我傳遞的價值是獲得相同的圖像5次。我需要加載不同的圖像在所有單元格..如果只有一個圖像意味着我需要加載該特定圖像在第一個單元格其他細胞應加載靜態圖像,可能是任何圖像..
我試過的代碼:
let json = JSON(data: data!)
let imgString = json["message"]["image"].stringvalue
let myData = imgString.componentsseperatedbystring(",")
我的數據源方法:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("editPendingCell", forIndexPath: indexPath) as! pendingEditCollectionViewCell
cell.myImage.sd_setImageWithURL(NSURL(string:pendingImageString))
return cell
}
無關,JSON設計不好。 'image'應該是一個字符串數組,而不是一個字符串。你有什麼選擇來解決這個問題,而不是圍繞糟糕的設計進行編程?另外,我不知道'json [「message」] [「image」]'是如何工作的,因爲'message'是一個字典數組。它應該是'json [「message」] [0] [「image」]''。 – Rob
問題在於'pendingImageString'。但是你沒有向我們展示你設置的位置,所以有點難以告訴你如何解決它,除了注意到你可能應該引用混淆地命名爲'myData'而不是'pendingImageString'的數組。 – Rob
也無關緊要,但類應以大寫字母開頭,例如'PendingEditCollectionViewCell',而不是'pendingEditCollectionViewCell'。 – Rob