UICollectionViewCell 错误

UICollectionViewCell error

我的 json 数据就是这样,我正在使用 alamofire 加载数据和 mapping.I 的 objectmapper 只是尝试解析 json 数据并将其显示在 CollectionView.However 我收到错误。

这是我的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()
        }
    }


}

这是我的映射计划文件代码。

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"]
    }
}

当我尝试 运行 时,出现这样的错误:类型 'UICollectionViewCell' 的值没有成员 'textLabel'

我尝试将 "textLabel" 添加到 viewcontroller,但没有成功 work.Should 我为 CollectionView 添加了一个新的 class?

这是一个 collectionview 单元格 class 你可以使用它有 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)

   }
}

您现在可以注册这个单元格并使用它,这样您就可以使用 textLabel 属性。