Xcode如何声明我的单元格来显示我的UITableViewCell?

问题描述 投票:0回答:1

/ 文件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
    }

}
arrays swift xcode iboutlet
1个回答
0
投票
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
}

也可以从故事板中为单元格分配标识符。


0
投票

你是说给他们登记吗?这将是 tableView.registerCell(MissionTableViewCell.self)

© www.soinside.com 2019 - 2024. All rights reserved.