Bako Abdullah
Bako Abdullah

Reputation: 105

grab data from api for UICollectionView inside UICollectionViewCell using alamofire and swiftyJson

hello I am working on an application for retrieving data about movies but I have a problem , I am using a CollectionView for displaying movie categories and inside these categories (inside the Collection View Cells) I am using another collection View to display movies according to the movie category but I can't populate the cells of the second collection view because it is a subclass of custom cell and I have also created a xib file for the custom cell and I am trying to implement delegate and datasource methods for the second collection view inside the class that I have made for the custom cell this is a part of my code :

func parseJson(category:MovieCategory)
   {


       let currentCategory = category
       let parameters = ["api_key":"8460d476d21be7e26a99234d8ca8de51","sort_by":"false","include_video":"false","with_genres":currentCategory.genreID]

       Alamofire.request(AppModel().url, method: .get, parameters: parameters).responseJSON { (response) in

           if response.result.isSuccess{
               let movieJSON = JSON(response.result.value!)

               for i in 0..<20{

                   let title = movieJSON["results"][i]["title"].stringValue
                   let posterPath = movieJSON["results"][i]["poster_path"].stringValue
                   let overview = movieJSON["results"][i]["overview"].stringValue
                   let dateReleased = movieJSON["results"][i]["release_date"].stringValue
                   let voteAverage = movieJSON["results"][i]["vote_average"].stringValue
                 print(title)
                   if let safeImage = AppModel().createImage(posterPath: posterPath)
                   {
                       let movieDetails = MovieDetails(movieTitle: title, poster: safeImage, voteAverage: voteAverage, overview: overview, releaseDate: dateReleased)

                       currentCategory.categoryMovies.append(movieDetails)




                   }

               }

           }

       }
   }

and the where I want to use the retrieved data from the api:

class CategoryCollectionViewCell:UICollectionViewCell,UICollectionViewDelegate,UICollectionViewDataSource{

@IBOutlet weak var sectionName: UILabel!

@IBOutlet weak var movieCollectionView: UICollectionView!

var sectionNumber:Int = 0

let appModel = AppModel()

override func awakeFromNib() {
    super.awakeFromNib()
    movieCollectionView.delegate = self
    movieCollectionView.dataSource = self

    movieCollectionView.register(UINib(nibName: "CollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "cell")



}



@IBAction func viewAllPressed(_ sender: UIButton) {
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
    appModel.movieCategories.count
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return appModel.movieCategories[section].categoryMovies.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = movieCollectionView.dequeueReusableCell(withReuseIdentifier: "categoryCell", for: indexPath) as! CollectionViewCell
    cell.posterImage.image = appModel.movieCategories[indexPath.section].categoryMovies[indexPath.item].poster
    return cell

}

} these are the model classes:

class MovieDetails{
let movieTitle:String
let poster:UIImage
let voteAverage:String
let overview:String
let releaseDate:String
init(movieTitle:String,poster:UIImage,voteAverage:String,overview:String,releaseDate:String) {
    self.movieTitle = movieTitle
    self.overview = overview
    self.poster = poster
    self.releaseDate = releaseDate
    self.voteAverage = voteAverage
}

}

class MovieCategory {
var genreID:String = ""
var categoryTitle:String = ""
var categoryMovies:[MovieDetails] = []
init(genreID:String,categoryTitle:String) {
    self.genreID = genreID
    self.categoryTitle = categoryTitle
}

}

I think my problem is that I don't know where to call parseJson in side CategoryCollectionViewCell and where to call reloadData(). if you know any thing about that please help me thankYou.

Upvotes: 1

Views: 960

Answers (1)

NSMohammad
NSMohammad

Reputation: 131

thinks its work: write in ur alamofire func and after all of (.success) & (.failure)

self.yourCollectionView.reloadData()

Upvotes: 1

Related Questions