首页 > 解决方案 > 如何在一个 UITableView 中显示 4 列数据,每个单元格使用 4 个不同的标签

问题描述

我目前正在开发一个应用程序,该应用程序允许用户将数据输入到表单中,并且数据将存储在本地的 coredata 中。管理员将在单元格中的表格视图中显示数据。有四种类型的数据必须记录到表格单元格中。我尝试在一个单元格中使用四个标签以允许将数据打印到单元格中。这是我现在的表格视图

这是我现在的一些代码。

第一部分是关于代码如何提取数据并分配给四个标签。

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let person = people[indexPath.row]
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! Table_VC_Cell

        cell.nameLabel.text = person.value(forKey: "name") as? String
        cell.placeLabel.text = person.value(forKey: "place") as? String
        cell.contactnoLabel.text = person.value(forKey: "contactno") as? String
        cell.purposeLabel.text = person.value(forKey: "purposeofvisit") as? String
        return cell
    }


}

这部分是标签和代码的连接。

    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var placeLabel: UILabel!
    @IBOutlet weak var contactnoLabel: UILabel!
    @IBOutlet weak var purposeLabel: 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
    }

}

每当我在表单中输入数据后显示表格时,应用程序就会崩溃。Xcode 特别告诉我错误是由于这段代码造成的。我应该如何解决这个问题?

标签: swiftxcode

解决方案


推荐阅读