ios – Swift:自定义单元格中的IBoutlet为零

我无法弄清楚为什么没有输出这个自定义单元格.

我在故事板中设置了自定义单元格(没有笔尖).
我有一个文本字段和2个标签,当我尝试在自定义单元格类中访问它们时为nil.我几乎可以肯定我已经把所有东西都搞定了,但仍然没有.

我在故事板中选择了我的表格单元格,并将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”,否则您将崩溃.)

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...