简体   繁体   English

如何在索引路径的行中显示单元格中的数据模型内容?

[英]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.

相关问题 Swift:如何获取表行单元格(indexPath)并从详细视图控制器的数据模型中加载它? - Swift: How to get the table row cell (indexPath) and load it from the data model in detail view controller? 如何在“索引路径中的行单元”之外访问uitableview单元元素 - how to access uitableview cell element outside of “cell for row at indexpath” 如何在Swift中的collectionView中传递indexPath行数据 - How to pass indexPath row data in collectionView in Swift 在单元格中获取indexPath.row - Get indexPath.row in cell 如何将拖动的单元格的indexPath.row传递给函数? (迅速) - How to pass indexPath.row of the dragged cell into a function? (SWIFT) 轻按单元格中的UIImageView时如何获取indexpath.row? - How to get the indexpath.row when a UIImageView in a cell is tapped? 如何使用indexPath访问自定义单元中的UiLabel并将用户数据实现到标签中 - How to access UiLabel in custom cell with indexPath and implement user data into the label 如何通过按钮更新在自定义单元格中分配给索引路径的核心数据 - How to update Core Data assigned to indexpath in Custom cell via button 如何获取当前单元格的indexPath - How to get the indexPath of the current cell 如何在单元格类中获取indexPath? - How to get indexPath in cell class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM