2
votes

I am very new to swift development and I am working on this section of an apple provided swift tutorial. I created an outlet for a label, image, and custom view that are nested in a Table Cell. When I run I get this error

2016-07-20 23:16:11.110 FoodTracker[8446:3016336] Unknown class MealTableViewCell in Interface Builder file. 2016-07-20 23:16:11.124 FoodTracker[8446:3016336] * Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key nameLabel.' * First throw call stack:

There are no duplicate outlets and I believe the cellIdentifier used in my Table View Controller is correct. My table cell

import UIKit

class MealTableViewCell: UITableViewCell {

//    MARK: Properties
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var photoImageView: UIImageView!
@IBOutlet weak var ratingControl: RatingControl!

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

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

My Table View Controller

import UIKit

class MealTableViewController: UITableViewController {
// MARK: Properties

var meals = [Meal]()

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

func loadSampleMeals() {
    let photo1 = UIImage(named: "meal1")!
    let meal1 = Meal(name: "Caprese Salad", photo: photo1, rating: 4)!

    let photo2 = UIImage(named: "meal2")!
    let meal2 = Meal(name: "Chicken and Potatoes", photo: photo2, rating: 5)!

    let photo3 = UIImage(named: "meal3")!
    let meal3 = Meal(name: "Pasta with Meatballs", photo: photo3, rating: 3)!

    meals += [meal1, meal2, meal3]
}

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

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

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    // Table view cells are reused and should be dequeued using a cell identifier.
    let cellIdentifier = "MealTableViewCell"
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! MealTableViewCell

    // Fetches the appropriate meal for the data source layout.
    let meal = meals[indexPath.row]

    cell.nameLabel.text = meal.name
    cell.photoImageView.image = meal.photo
    cell.ratingControl.rating = meal.rating

    return cell
}}

Thanks!

3
Have you set class of RatingControl in storyboard??Vishal Sonawane

3 Answers

10
votes

Check for a broken outlet link in your storyboard file. Find your controller and control-click on the yellow circle above its nib to open the actions and outlets popover. You should see an outlet for nameLabel with a yellow warning sign. Either reconnect it to your controller's file or remove it if its a duplicate.enter image description here

2
votes

Its very simple , the story board can not find the outlet with the name "nameLabel". So either create a label by that name or delete the reference of the created label from the storyboard.

Here are the steps

Just click the storyboard and select the last Tab.enter image description here

Now search for the nameLabel and click on cross to disconnect. All the best ;)

0
votes

First, you must link your UITableView from your interface builder to your MealTableViewController class ( Ctrl + drag )

Then, in the viewDidLoad method of your MealTableViewController class, you must register your custom cell class to your table view :

self.tableView.registerClass(MealTableViewCell.self, forCellReuseIdentifier: "MealTableViewCell")