Value of type 'UITableViewCell' has no member 'nameLabel'

I can't figure out how to solve the error: Value of type 'UITableViewCell' has no member 'nameLabel'

The line that throws this error in the code below is: cell.nameLabel.text = item.name, which is at the bottom just above the return cell line.

This may or may not be useful to know, but this is a lesson in a Sololearn course for Swift.

import UIKit

class ItemTableViewController: UITableViewController {
    var items = [Item]()

    func loadSampleItems() {
        items += [Item(name: "item1"), Item(name:
        "item2"), Item(name: "item3")]
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        loadSampleItems()
    }

    override func numberOfSections(in tableView:
    UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView,
    numberOfRowsInSection section: Int) -> Int {
        return items.count
    }

    override func tableView(_ tableView: UITableView,
    cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell =
        tableView.dequeueReusableCell(withIdentifier:
        "ItemTableViewCell", for: indexPath)

        let item = items[indexPath.row]
        cell.nameLabel.text = item.name
        return cell
    }
Answered by devinkkperry in 683984022

Found the problem, I was supposed to add as! ItemTableViewCell at the end of the let cell line.

Your cell is of type UITableViewCell.
UITableViewCell has no property called nameLabel.

Perhaps you expect cell to be an ItemTableViewCell?
You don't show your code, so I'm guessing, but perhaps ItemTableViewCell has a property called nameLabel?
If so, when you dequeue your cell, you would have to cast it to the correct type.

@robnotyou I think this is the code you're referring to that might be helpful. It's in a file called ItemTableViewCell.swift

import UIKit

class ItemTableViewCell: UITableViewCell {
    @IBOutlet weak var nameLabel: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }
} 
Accepted Answer

Found the problem, I was supposed to add as! ItemTableViewCell at the end of the let cell line.

Value of type 'UITableViewCell' has no member 'nameLabel'
 
 
Q