Ios 如何在indexpath的行的单元格中显示数据模型内容?
您好,我正在尝试在tableview中显示特定对象的名称和pointsWorth。但是xcode answers missionTitleLabel不能用于missionCell。可以显示我创建的对象的信息吗 感谢我能得到的任何帮助 这是我的密码: MasterViewController.swift:Ios 如何在indexpath的行的单元格中显示数据模型内容?,ios,swift,uitableview,swift3,didselectrowatindexpath,Ios,Swift,Uitableview,Swift3,Didselectrowatindexpath,您好,我正在尝试在tableview中显示特定对象的名称和pointsWorth。但是xcode answers missionTitleLabel不能用于missionCell。可以显示我创建的对象的信息吗 感谢我能得到的任何帮助 这是我的密码: MasterViewController.swift: override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITabl
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
}
示例数据模型:
枚举类别{
案例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
}
}let mission1=任务(名称:“a”,pointsWorth:50,颜色主题:。蓝色,描述:“a是字母表中的第一个字母”,类别:。a)
let mission2=任务(名称:“b”,pointsWorth:60,颜色主题:。红色,描述:“b是字母表中的第二个字母”,类别:.b)
var任务:[任务]=[任务1,任务2]
MissionCell.swift:
import UIKit
类任务单元: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)
}
}您应该这样分配值:
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
}
在这里,您应该使用
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
}
希望能有所帮助。谢谢您的回答!完成此操作后,我得到以下消息:无法将类型Mission的值分配给类型String?有什么方法可以访问objects name属性吗?您应该使用
mission.name
谢谢orxelm!这删除了错误,但在我运行应用程序时,该名称未显示在tableview中。您可以共享屏幕截图吗?您是否看到了正确数量的单元格,但它们只是空的?可能您没有将标签连接到单元格标签?一定是出了问题,因为第二次运行应用程序时,我收到了一个sigbart原因:“无法将标识符为MissionCell的单元格出列-必须为标识符注册nib或类,或在情节提要中连接原型单元格”什么类型的任务
?它是一根绳子吗?是的,它是一根绳子谢谢!您的代码删除了错误,但在我运行应用程序时,名称未显示在tableview中。您能否在覆盖func tableview(\uTableView:UITableView,cellForRowAt indexPath:indexPath)->UITableViewCell
中进行调试?代码在这里触发了吗?嘿,你能检查一下你的故事板吗?您应该像这样将delegate
和datasource
链接到您的viewcontroller。似乎我的委托和数据源链接到了我的视图控制器[2]:设置断点时,我可以看到以下方法没有运行:在indexpath configureView中为行的segue单元格做准备+数据模型:dataModel.swift