Mark Illingworth
Mark Illingworth

Reputation: 1

'UITableViewCell' does not have a member named 'nameLabel'

I'm using a custom nib to load into a UITableView and trying to access the custom labels - but can't seem to get them to find the member! What am I doing wrong?

updated code 15/4 for completeness

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.rowHeight = 80

// registered the nib  
var cellNib = UINib(nibName: "IndividualResultCell", bundle: nil)
tableView.registerNib(cellNib, forCellReuseIdentifier:     
                                          "IndividualResultCell")

    self.monthView = [

    Month(name: "Darren Jensen", salesrev: 15000, gp: 5000,  
          gppercent: 33.3),
    Month(name: "Duncan Lane", salesrev: 17500, gp: 5000, 
          gppercent: 28.6)]

     // Reload the table
    self.tableView.reloadData()
       }

    // downcast the cell to the custom class
  override func tableView(tableView: UITableView, cellForRowAtIndexPath
  indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier
      ("IndividualResultCell", forIndexPath: indexPath) as!
        UITableViewCell

    let month = self.monthView[indexPath.row]

    // Error message here ... nameLabel not a member of UITableViewCell
    cell.nameLabel?.text = month.name
    cell.salesrevLabel?.text = month.salesrev

    return cell
}

---- the custom class

import UIKit

  class IndividualResultCell: UITableViewCell {

    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var salesrevLabel: UILabel!
    @IBOutlet weak var gpLabel: UILabel!
    @IBOutlet weak var gppercentLabel: UILabel!

 }

IndividualResultCell.xib - have checked the reuse identifier - it is IndividualResultCell

Upvotes: 0

Views: 414

Answers (2)

Kevin Horgan
Kevin Horgan

Reputation: 1580

Register your NIB in the viewDidLoad function first...

    @IBOutlet var tableView: UITableView
    override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.registerNib(UINib(nibName: "IndividualResultCell", bundle: nil), forCellReuseIdentifier: "IndividualResultCell")
    }

Then try to dequeue the custom cell like this...

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("IndividualResultCell", forIndexPath: indexPath) as! IndividualResultCell

EDIT 1 - Array Definition Regarding your comment below, I assume you have Month defined somthing like this in your code...

class Month  {
var name = String()
var salesrev = Int()
var gp = Int()
var gppercent = Double()
}

I would then define your array like this and see if that fixes the problem you have populating your tableview cell. The problem where the println command shows you are returning an unexpected value from the array.

var monthView : Array = []

var theMonth = Month()
theMonth.name = "Darren Jensen"
theMonth.salesrev = 15000
theMonth.gp = 5000
theMonth.gppercent = 33.3

monthView.append(theMonth)

theMonth.name = "Duncan Lane"
theMonth.salesrev = 17500
theMonth.gp = 5000
theMonth.gppercent = 28.6

monthView.append(theMonth)

Upvotes: 0

zisoft
zisoft

Reputation: 23078

You need to downcast the cell to your custom class:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("IndividualResultCell", forIndexPath: indexPath) as! IndividualResultCell
    ...
}

Assuming that you assigned the class to the prototype cell in InterfaceBuilder or registered the class properly if you do that in code.

Upvotes: 3

Related Questions