Я создал UICollectionView
Внутри ViewController
, но UICollectionView
не загружает данные, которые я добавил в массив.Я пытался распечатать из источника данных UIcollectionView
и делегата, но он также не печатал
import UIKit
import Alamofire
import SwiftyJSON
import SDWebImage
class SelectCarViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
@IBOutlet weak var selectedcarManufacturalLebel: UILabel!
@IBOutlet weak var selectedcarPlateNumber: UILabel!
@IBOutlet weak var selectCarCarouselCollectionView: UICollectionView!
var selecrCarsURL = "https://my.api.mockaroo.com/cars.json?key=86f86980"
var selectCarArray : [SelectCarDataModel] = [SelectCarDataModel]()
let selectCarFromJSON = SelectCarDataModel()
override func viewDidLoad() {
super.viewDidLoad()
selectCarCarouselCollectionView.delegate = self
selectCarCarouselCollectionView.dataSource = self
//register custom sell nib file
selectCarCarouselCollectionView.register(UINib.init(nibName: "SelectCarViewCell", bundle: nil), forCellWithReuseIdentifier: "selectIdentifier")
//Add flowyout function
addFlowlayOut()
getselectCarData(url: selecrCarsURL)
selectCarCarouselCollectionView.reloadData()
}
//MARK:- Add carousel flowlayout for viewcontroller collectionview
func addFlowlayOut() {
let flowlayout = UPCarouselFlowLayout()
flowlayout.itemSize = CGSize(width: UIScreen.main.bounds.size.width - 60, height: selectCarCarouselCollectionView.frame.size.height)
flowlayout.scrollDirection = .horizontal
flowlayout.sideItemScale = 0.8
flowlayout.sideItemAlpha = 1.0
flowlayout.spacingMode = .fixed(spacing: 5.0)
selectCarCarouselCollectionView.collectionViewLayout = flowlayout
}
//MARK:- Select Car Newtworking
func getselectCarData(url: String) {
Alamofire.request(url, method: .get).responseJSON {
response in
if response.result.isSuccess {
print("Sucess Got the Selected Cars Data")
let selelectCarJSON : JSON = JSON(response.result.value!)
print(selelectCarJSON)
self.updateSelectedCar(json: selelectCarJSON)
}else {
print("error)")
}
}
}
//MARK:- Select Car Update JSON Parsing
func updateSelectedCar(json : JSON) {
//for i in 0...json.count-1 {
selectCarFromJSON.image = json["car"][0]["img"].stringValue
selectCarFromJSON.carType = json["car"][0] ["manufacturer"].stringValue
selectCarFromJSON.carModel = json["car"][0]["model"].stringValue
print("this is it \(selectCarFromJSON.image)")
//}
}
//MARK: -- UICollection Delegate and Datasourse Manipulation
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return selectCarArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = selectCarCarouselCollectionView.dequeueReusableCell(withReuseIdentifier: "selectIdentifier", for: indexPath) as! SelectCarViewCell
let selectCar = SelectCarDataModel()
selectCar.image = selectCarFromJSON.image
selectCar.carType = selectCarFromJSON.carType
selectCar.carModel = selectCarFromJSON.carModel
selectCarArray.append(selectCar)
print(selectCarArray)
cell.selectCarImage.sd_setImage(with: URL(string: selectCarArray[indexPath.row].image))
selectedcarManufacturalLebel.text = selectCarArray[indexPath.row].carType
selectedcarPlateNumber.text = selectCarArray[indexPath.row].carModel
print("this is cell\(selectCarArray[indexPath.row].image)")
selectCarCarouselCollectionView.reloadData()
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print(indexPath.row)
}
}
. Я ожидал, что SDWebImage
получит URL-адрес из JSON
и заполнит UICollectionView