Tromben
Tromben

Reputation: 3

How to Display data model content in cell for row at indexpath?

enter image description herehttps://i.sstatic.net/pRZu5.png Hi I'm trying to display the name and the pointsWorth of a specific object in a tableview. But the xcode answers missionTitleLabel cannot be used on 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:

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 {

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 mission2 = Mission(name: "b", pointsWorth: 60, colorTheme: .red, description: "b is the second letter in the alphabet", category:.b)

var missions: [Mission] = [mission1, mission2]

MissionCell.swift:

import UIKit

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)

}

}

Upvotes: 0

Views: 783

Answers (2)

orxelm
orxelm

Reputation: 1144

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. Consider:

if let cell = cell as? MissionCell {
  cell.missionTitleLabel?.text = mission.name

}

Upvotes: 0

Luan Tran
Luan Tran

Reputation: 1142

Here you should use cell variable as 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.

Upvotes: 0

Related Questions