Yash R Yash R - 3 years ago 128
JSON Question

Multiple images from JSON array to UICollectionView

I am using tabman for tab like layout. In my case there are 4 tabs which contain 2 to 3 images each. I am showing that images in

UICollectionView
.

My JSON:

"PhotoList": [
{
"ID": 0,
"DaySeq": 1,
"ImgPath": "Images\\1.jpg"
},
{
"ID": 0,
"DaySeq": 1,
"ImgPath": "Images\\2.jpg"
},
{
"ID": 0,
"DaySeq": 2,
"ImgPath": "Images\\3.jpg"
},
{
"ID": 0,
"DaySeq": 2,
"ImgPath": "Images\\4.jpg"
},
{
"ID": 0,
"DaySeq": 3,
"ImgPath": "Images\\.jpg"
},
{
"ID": 0,
"DaySeq": 4,
"ImgPath": "Images\\6.jpg"
},
{
"ID": 0,
"DaySeq": 5,
"ImgPath": "Images\\7.jpg"
}
]


I want in tab one there is a
UICollectionView
and I want to display only that images which having 'DaySeq' 1. As like this in other tab I want images with 'DaySeq' 2.

What I am trying in code:

override func viewDidLoad() {
super.viewDidLoad()
Alamofire.request("http://bestapp.in/api/Package/GetItineraryByPackageID/2").responseJSON(completionHandler: { (responseData) in

if ((responseData.result.value) != nil) {

let swiftyJSONVar = JSON(responseData.result.value!)
if let resData = swiftyJSONVar["DayDescrList"].arrayObject,
let resImg = swiftyJSONVar["PhotoList"].arrayObject
{

self.arrRes = resData as! [[String:AnyObject]]
self.arrImg = resImg as! [[String:AnyObject]]

self.dataSource = self
self.bar.location = .top
}
}
})
}
func viewControllers(forPageboyViewController pageboyViewController: PageboyViewController) -> [UIViewController]? {
let storyboard = UIStoryboard(name:"Main",bundle:Bundle.main)

var viewControllers = [UIViewController] ()
var tabBarItems : [TabmanBar.Item] = []

for j in 0 ..< self.arrImg.count {
for var k in 0 ..< j {
k += 1
if k == self.arrImg[j]["DaySeq"] as! Int {
let vc = storyboard.instantiateViewController(withIdentifier: "childvc") as! childvc
vc.imgURL = self.arrImg[j]["ImgPath"] as! String
print(self.arrImg[j]["ImgPath"] as! String)
self.array.append(self.arrImg[j]["ImgPath"] as! String)
}

print(array)
}

}
print(array)
let vc = storyboard.instantiateViewController(withIdentifier: "childvc") as! childvc
vc.array = array
self.bar.items = tabBarItems
return viewControllers

}

Answer Source
  1. Create a model example:

    class ImagesModel {
      var id: Int?
      var path: String?
      var daySeq: Int?
    
      func parseWith(json: JSON){
         self.id = json["ID"].intValue
         self.path = json["ImgPath"].intValue
         self.daySeq = json["DaySeq"].intValue
      }
    }
    
  2. Create array of ImagesModel in your class:

    var imagesModelArray = [ImagesModel]()
    
  3. Just parse your array into this model example:

    let photosListArray = json["PhotoList"].arrayValue
    for objects in photosListArray{
       let model = ImagesModel()
       model.parseWith(json: objects)
       self.imagesModelArray.append(model)
    }
    
  4. After that in collection View numberOfItems method you need to just sort data according to there tab.

    func collectionView(_ collectionView: UICollectionView, 
         numberOfItemsInSection section: Int) -> Int {
         if tab == 0{
             return self.imagesModelArray.filter({$0.daySeq == 0})
         }else if tab == 1{
             return self.imagesModelArray.filter({$0.daySeq == 1})
         }
         ///MORE CONDITIONS
         //At the End
         return 0
    }
    

After that make these type of filtered Arrays cellForItem and get image from these filtered array.

Enjoy Swift.

Maybe it's Helpful. If any query please comment

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download