Reputation: 1443
I have a small problem. I only know how to parse JSON image inks to collection cell if JSON came as "key": "value" But this time I got the type of JSON on the link below...
The trouble is... I have multiples links of images on array ["link", "link", "link"]. How I can put 3-4 images from the first array in one collection view (screenshot below) and then fill other cells
The structure is.. one tableView -> on each cell of TableView I put CollectionView -> Collection has cells with image block with horizontal scroll and title
Screenshot below:
https://d.radikal.ru/d30/1803/60/35717754afbd.png
{ "list": [
{
"title" : "iPhone 5s",
"images": [
"https://upload.wikimedia.org/wikipedia/commons/f/fd/IPhone_5S.jpg",
"http://img01.ibnlive.in/ibnlive/uploads/2015/12/apple-iphone5s-151215.jpg",
"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcR5cv-olMz3XKQNhQP4SpwiwqtiDreaBlpESHdCDc6Jm5GjHzRsHcxXrqAI"
]
},
{
"title" : "iPhone 6s",
"images": [
"https://c1.staticflickr.com/2/1665/26162561181_01148e99ee_b.jpg",
"https://img1.ibay.com.mv/is1/full/2017/11/item_2028958_545.jpg",
"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSz8jIpCOU94HxZEab_vJdl9nGsaAOO18dqq2BXt_L2-PnWhroi",
"https://c1.staticflickr.com/4/3907/15102682838_25e6c90469_b.jpg",
"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTnMHHcDnMlsrtPpZmfLjQqlJXQNNEvTTg7WWMGcbOHOvxdVUoi"
]
},
{
"title" : "iPhone 7",
"images": [
"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRQYu7fKAuYLwQCiilRNCv_wzVZbOpLGsrRzQA7prdgToCiBzsQ",
"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSGS8lca49LZGvPUtJxrof6DuzvjgKiR_0Nei_b8zeR-3uq1kzyLQ",
"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSGS8lca49LZGvPUtJxrof6DuzvjgKiR_0Nei_b8zeR-3uq1kzyLQ",
"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTko9xdQRKcdJrshSPWCjTtml9eSKiABSN--VhC5YV8MMASVRfgYw",
"https://cdn.pixabay.com/photo/2014/12/10/12/27/iphone-563070_960_720.jpg"
]
}
]
}
Extension to load photo with a link
extension UIImageView{
func downloadImg(from url: String){
let urlRequest = URLRequest(url: URL(string: url)!)
let task = URLSession.shared.dataTask(with: urlRequest) { (data,response,error) in
if error != nil{
print(error)
return
}
DispatchQueue.main.async {
self.image = UIImage(data: data!)
}
}
task.resume()
}
}
My object structure
struct info : Decodable {
let list : [lists]
}
struct lists : Decodable{
let title : String?
let images : [String]?
}
Usually, if JSON has a struct-like "key":"value" I do this.
cell.productImage.downloadImg(from: (arrayImg?[indexPath.row].url)!)
Upvotes: 0
Views: 824
Reputation: 285072
First of all please conform to the naming convention that struct names start with a capital letter.
You can decode the URL strings directly to URL and don't declare the properties as optionals if the JSON contains all keys
struct Info : Decodable {
let list : [List]
}
struct List : Decodable {
let title : String
let images : [URL]
}
Decode the JSON and iterate over the array (data
is the received Data
instance)
do {
let result = try JSONDecoder().decode(Info.self, from: data)
for phone in result.list {
for imageURL in phone.images {
print(imageURL)
}
}
} catch { print(error) }
Upvotes: 1