Xcode Как объявить мои ячейки для отображения моего UITableViewCell? - PullRequest
0 голосов
/ 17 июня 2020

// файл 1 = MissionTableViewController.swift

import UIKit

class MissionTableViewController: UITableViewController {

    var missionToutes = [

    Mission(title: "AAAAA", number: "1", description: "aaaaaa aaaaa aaaaa"),
    Mission(title: "BBBBB", number: "2", description: "bbbbbb bbbbb bbbbb"),
    Mission(title: "CCCCC", number: "3", description: "cccccc ccccc ccccc"),
    Mission(title: "DDDDD", number: "4", description: "dddddd ddddd ddddd"),
    Mission(title: "EEEEE", number: "5", description: "eeeeee eeeee eeeee")

    ]


    struct Mission {
        var title: String
        var number: String
        var description: String
    }

    var currentIndex = 0


    override func viewDidLoad() {
        super.viewDidLoad()
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return missionToutes.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellIdentifiant = "Cell"
        let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifiant, for: indexPath) as! MissionTableViewCell
        let mission = missionToutes[currentIndex]

        cell.labelTitle.text = mission.title
        cell.labelNumber.text = mission.number
        cell.labelDescription.text = mission.description

        return cell
    }

Файл 2: MissionTableViewCell.swift

import UIKit

class MissionTableViewCell: UITableViewCell {

    @IBOutlet weak var labelNumber: UILabel!
    @IBOutlet weak var labelTitle: UILabel!
    @IBOutlet weak var labelDescription: UILabel!


    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

Ответы [ 2 ]

0 голосов
/ 17 июня 2020

Вы имеете в виду зарегистрировать их? Это будет tableView.registerCell(MissionTableViewCell.self)

0 голосов
/ 17 июня 2020
override func viewDidLoad() {
    super.viewDidLoad()

 }
let cellIdentifiant = "Cell"
// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return missionToutes.count
}

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifiant, for: indexPath) as! MissionTableViewCell
    let mission = missionToutes[indexPath.row]

    cell.labelTitre.text = mission.titre
    cell.labelNumero.text = mission.nombre
    cell.labelDescription.text = mission.description

    return cell
}

также назначить ячейке идентификатор ячейки из раскадровки

...