Swift:IBoutlets在Custom Cell中是零

我不知道为什么这个自定义单元格不被输出。

我有一个自定义的单元格设置在故事板(无笔尖)。 我有一个文本字段和2个标签,当我尝试在自定义单元类中访问它们时为零。 我几乎可以肯定,我已经把所有的东西都正确地连接起来了,但是仍然是零。

我在故事板中select了我的表格单元格,并将Custom Class设置为TimesheetTableViewCell我也控制单击表格并将数据源和委托设置为TimesheetViewController

我的自定义单元类:

 import UIKit class TimesheetTableViewCell: UITableViewCell { @IBOutlet var duration: UITextField! @IBOutlet var taskName: UILabel! @IBOutlet var taskNotes: UILabel! required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder) } override init?(style: UITableViewCellStyle, reuseIdentifier: String!) { super.init(style: style, reuseIdentifier: reuseIdentifier) println("Cell's initialised")// I see this println(reuseIdentifier)// prints TimesheetCell } override func setSelected(selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) } func setCell(duration: String, taskName: String, taskNotes: String){ println("setCell called") self.duration?.text = duration self.taskName?.text = taskName self.taskNotes?.text = taskNotes } 

我的控制器:

 class TimesheetViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{ @IBOutlet var timesheetTable: UITableView! var items = ["Item 1", "Item2", "Item3", "Item4"] override func viewDidLoad() { super.viewDidLoad() self.timesheetTable.registerClass(TimesheetTableViewCell.self, forCellReuseIdentifier: "TimesheetCell") } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("TimesheetCell", forIndexPath: indexPath) as TimesheetTableViewCell println(items[indexPath.row]) // prints corresponding item println(cell.duration?.text) // prints nil cell.setCell(items[indexPath.row], taskName: items[indexPath.row], taskNotes: items[indexPath.row]) return cell } 

问题是这一行:

 self.timesheetTable.registerClass(TimesheetTableViewCell.self, forCellReuseIdentifier: "TimesheetCell") 

删除它。 该行说:“不要从故事板中获取细胞。” 但是你确实想从故事板中获取细胞。

(但是,请确保故事板中单元格的标识符是"TimesheetCell" ,否则会崩溃。)

很确定,你需要删除该行

 self.timesheetTable.registerClass(TimesheetTableViewCell.self, forCellReuseIdentifier: "TimesheetCell") 

from viewDidLoad()。