'UITableViewCell'没有名为'nameLabel'的成员

时间:2015-04-13 12:39:11

标签: ios uitableview swift

我使用自定义笔尖加载到UITableView并尝试访问自定义标签 - 但似乎无法让他们找到该成员!我做错了什么?

更新代码15/4以获得完整性

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
}

----自定义类

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 - 检查了重用标识符 - 它是IndividualResultCell

1 个答案:

答案 0 :(得分:0)

首先在viewDidLoad函数中注册您的NIB ...

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

然后尝试像这样将自定义单元格出列...

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

编辑1 - 数组定义 关于下面的评论,我假设您在代码中有月份定义的内容......

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

然后我会像这样定义你的数组,看看是否能解决你填充tableview单元格的问题。 println命令显示的问题是从数组返回一个意外的值。

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)