[英]How to Display data model content in cell for row at indexpath?
enter image description here https://i.stack.imgur.com/pRZu5.png Hi I'm trying to display the name and the pointsWorth of a specific object in a tableview. 在这里输入图像描述 https://i.stack.imgur.com/pRZu5.png您好我正在尝试在表格视图中显示特定对象的名称和pointsWorth。 But the xcode answers missionTitleLabel cannot be used on missionCell.
但是xcode回答missionTitleLabel不能在missionCell上使用。 Is it possible to display information from the object that I've created?
是否可以显示我创建的对象中的信息?
Thankful for any help I can get! 感谢您能获得的任何帮助!
Here is my code: MasterViewController.swift: 这是我的代码: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
}
example Data Model: 示例数据模型:
enum CategoryEnum { case a case b case c case d } public class Mission { 枚举CategoryEnum {案例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 = Mission(name: "a", pointsWorth: 50, colorTheme: .blue, description: "a is the fist letter in the alphabet", category:.a) } let mission1 = Mission(name:“ a”,pointsWorth:50,colorTheme:.blue,描述:“ a是字母中的第一个字母”,类别:.a)
let mission2 = Mission(name: "b", pointsWorth: 60, colorTheme: .red, description: "b is the second letter in the alphabet", category:.b) let mission2 = Mission(名称:“ b”,点数:60,colorTheme:.red,描述:“ b是字母表中的第二个字母”,类别:.b)
var missions: [Mission] = [mission1, mission2] var任务:[任务] = [任务1,任务2]
MissionCell.swift: MissionCell.swift:
import UIKit
class MissionCell: UITableViewCell { class 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)
}
} }
You should assign the values like this: 您应该这样分配值:
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
}
Keep in mind that using the force cast to MissionCell is only when you know for sure that there's one kind of cell type, otherwise you will crash. 请记住,只有在确定存在一种类型的单元格时,才使用对MissionCell施加的力,否则会崩溃。 Consider:
考虑:
if let cell = cell as? MissionCell {
cell.missionTitleLabel?.text = mission.name
}
Here you should use cell
variable as MissionCell
. 在这里,您应该将像
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
}
Hope it help. 希望对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.