-1

My json data is like that and I'm using alamofire for loading data and objectmapper for mapping.I just try to parsing json data and show it on CollectionView.However Im getting error.

Here is my viewcontroller

import UIKit
import Alamofire
import ObjectMapper
import AlamofireObjectMapper

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return schedules?.count ?? 0

    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = UICollectionViewCell()
        cell.textLabel?.text = schedules?[indexPath.row].title

    }


    @IBOutlet weak var collectionViewData: UICollectionView!

    var schedules: [Schedule]?

    override func viewDidLoad() {
        super.viewDidLoad()

        collectionViewData.dataSource = self
        collectionViewData.delegate = self

        loadData()

    }
    func loadData() {
        let jsonDataUrl = "https://jsonplaceholder.typicode.com/posts"

        Alamofire.request(jsonDataUrl).responseJSON { response in
            self.schedules = Mapper<Schedule>().mapArray(JSONObject: response.result.value)
            self.collectionViewData.reloadData()
        }
    }


}

Here is my schedule file codes for mapping.

import Foundation
import ObjectMapper
import AlamofireObjectMapper

class Schedule: Mappable {

    var userId: String
    var id: String
    var title: String
    var body: String

    required init?(map: Map) {
        userId = ""
        id = ""
        title = ""
        body = ""
    }

    func mapping(map: Map) {
        userId         <- map["userId"]
        id             <- map["id"]
        title          <- map["title"]
        body           <- map["body"]
    }
}

When I try to run it I'm getting error like that: Value of type 'UICollectionViewCell' has no member 'textLabel'

I tried to add "textLabel" to viewcontroller but it didn't work.Should I add a new class for CollectionView?

  • `UICollectionViewCell` doesn't have any `textLabel` property you need to create one. You should follow any tutorial first that how you can use `collectionView`, would recommend you to visit https://www.raywenderlich.com/136159/uicollectionview-tutorial-getting-started – deoKasuhal Mar 17 '18 at 08:26

1 Answers1

0

Here is a collectionview cell class you can use that has textLabel

class MyCollectionView: UICollectionViewCell {

 var textLabel: UILabel!

  override init(frame: CGRect) {
    super.init(frame: frame)
    createViews()
  }

  required init?(coder aDecoder: NSCoder) {
    fatalError("Cannot initialize")
  }

 func createViews() {
   textLabel = UILabel(frame: .zero)
   textLabel.textAlignment = .center
   textLabel.translatesAutoresizingMaskIntoConstraints = false
   textLabel.backgroundColor = UIColor.black
   textLabel.textColor = UIColor.white
   textLabel.font = Font.menuText
   contentView.addSubview(textLabel)

   let views: [String: Any] = ["label": textLabel]

   let lHFormat = "H:|[label]|"
   let lVFormat = "V:[label]|"

   [lHFormat, lVFormat].forEach { format in
   let constraints = NSLayoutConstraint.constraints(withVisualFormat: format,
                                                           options: [],
                                                           metrics: nil,
                                                           views: views)
    contentView.addConstraints(constraints)

   }
}

You can now register this cell and use it so that you can use textLabel property.