enter image description here https://i.stack.imgur.com/pRZu5.png 嗨,我试图在tableview中显示特定对象的名称和pointsWorth。但xcode回答missionTitleLabel无法在missionCell上使用。是否可以显示我创建的对象的信息?
感谢我能得到任何帮助!
这是我的代码: MasterViewController.swift:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MissionCell", for: indexPath)
let event = self.fetchedResultsController.object(at: indexPath)
self.configureCell(cell, withEvent: event)
let mission = missions[indexPath.row]
MissionCell.missionTitleLabel?.text = mission
return cell
}
示例数据模型:
枚举CategoryEnum { 案例a 案例b 案例c 案例d } 公共阶层使命{
var name: String
var pointsWorth: Int
var colorTheme: UIColor
var description: String
var category: CategoryEnum
init(name: String, pointsWorth: Int, colorTheme: UIColor, description: String, category: CategoryEnum) {
self.name = name
self.pointsWorth = pointsWorth
self.colorTheme = colorTheme
self.description = description
self.category = category
}
}让mission1 = Mission(名字:" a",pointsWorth:50,colorTheme:.blue,描述:" a是字母表中的第一个字母",category:。一个)
让mission2 = Mission(名称:" b",pointsWorth:60,colorTheme:。red,description:" b是字母表中的第二个字母",类别:.b )
var mission:[Mission] = [mission1,mission2]
MissionCell.swift:
import UIKit
类MissionCell:UITableViewCell {
@IBOutlet weak var missionTitleLabel: UILabel!
@IBOutlet weak var missionPointLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
答案 0 :(得分:0)
您应该像这样分配值:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MissionCell", for: indexPath) as! MissionCell
let event = self.fetchedResultsController.object(at: indexPath)
self.configureCell(cell, withEvent: event)
let mission = missions[indexPath.row]
cell.missionTitleLabel?.text = mission.name
return cell
}
请记住,只有在您确定存在一种细胞类型时才使用施放到MissionCell的力量,否则您将崩溃。 考虑一下:
if let cell = cell as? MissionCell {
cell.missionTitleLabel?.text = mission.name
}
答案 1 :(得分:0)
在这里,您应该将cell
变量用作MissionCell
。
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MissionCell", for: indexPath) as! MissionCell
let event = self.fetchedResultsController.object(at: indexPath)
self.configureCell(cell, withEvent: event)
let mission = missions[indexPath.row]
cell.missionTitleLabel?.text = mission.name
return cell
}
希望有所帮助。