在自定义表格单元格中访问标签时无法解压缩.NOs Swift

时间:2014-07-15 10:44:11

标签: ios swift tableview custom-cell

我正在尝试在表格视图中显示自定义单元格中的内容。 我从一个主细节应用程序开始,并尝试自定义它。 我使用自定义单元格创建了一个新的xib,并将其连接到一个类并创建了所需的插座。

这是MasterView控制器(删除了我未编辑的所有功能)

import UIKit

class MasterViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        var nipName=UINib(nibName: "SimpleTableCell", bundle:nil)
        self.tableView.registerNib(nipName, forCellReuseIdentifier: "cell")
        self.tableView.registerClass(SimpleTableCell.self, forCellReuseIdentifier: "cell")


        //Create Add Button
        let addButton = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "insertNewObject:")
        self.navigationItem.rightBarButtonItem = addButton
        loadInitialData()
        tableView.reloadData()

    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var  cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as SimpleTableCell
        cell.loadItem(user:"user1", hoop: "hoop1", post: "There is text in the table")
        return cell
    }
}

这是自定义单元格的类

import UIKit

class SimpleTableCell : UITableViewCell{
    @IBOutlet var userLabel: UILabel
    @IBOutlet var hoopLabel: UILabel
    @IBOutlet var postLabel: UILabel


    func loadItem(#user: String, hoop: String, post: String) {
        userLabel = UILabel()
        hoopLabel = UILabel()
        postLabel = UILabel()
        userLabel.text = user
        hoopLabel.text = hoop
        postLabel.text = post 
    }

}

然而,我总是得到一个"不能打开optional.none"在userLabel的文本即将被更改时,在loadItem函数中 - 我没有得到它,因为我在尝试添加文本之前初始化标签 - 或者我错了吗?

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

确定以某种方式安装最新的xcode beta并重新创建项目解决了问题... 谢谢你的帮助。

答案 1 :(得分:-1)

在您的视图中加载您正在为相同的单元标识符注册相同单元的类和nib。如果你有一个xib在单元格中拖放3个标签并将它们连接到IBOutlets,同时删除行self.tableView.registerClass(SimpleTableCell.self, forCellReuseIdentifier: "cell")。如果您没有使用xib创建标签,请设置框架并将其作为方法init(style: UITableViewCellStyle, reuseIdentifier: String!)中的子视图添加到单元格中,同时删除行self.tableView.registerNib(nipName, forCellReuseIdentifier: "cell")