我得到了UITableViewCell,但是cell的元素是nil。为什么?

时间:2014-11-04 11:20:10

标签: uitableview swift

以下代码是清洁的&重新上一篇文章的版本。
参考:This class is not key value coding-compliant for the key...why?

import Foundation
import UIKit

var x = 1

struct DiaryItem {
    var title:String?
    var subTitle:String?
    var leftImage:UIImage?
    var rightImage:UIImage?
    init(title:String, subTitle:String) {
        self.title = title
        self.subTitle = subTitle
    }
}

class DiaryTableViewCell: UITableViewCell {
    @IBOutlet weak var TitleLabel: UILabel!
    @IBOutlet weak var SubTitleLabel: UILabel!
    @IBOutlet weak var leftImageView: UIImageView!
    @IBOutlet weak var rightImageView: UIImageView!
}


class DiaryTableViewController: UITableViewController {
    let kCellIdentifier = "DiaryCell"
    var diaryCell:DiaryTableViewCell?
    var objects = NSMutableArray()  //...global var.

    override func viewDidLoad() {
        self.title = "My Diary"
        tableView.registerClass(DiaryTableViewCell.self, forCellReuseIdentifier: kCellIdentifier)
    }

    // -----------------------------------------------------------------------------------------------------
    // MARK: - UITableViewDelegate

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let controller = gStoryboard.instantiateViewControllerWithIdentifier("DiaryPlayerVC") as DiaryPlayerViewController
        self.navigationController?.pushViewController(controller, animated: true)
    }

    // -----------------------------------------------------------------------------------------------------
    // MARK: - UITableViewDataSource

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    // -----------------------------------------------------------------------------------------------------
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }

    // -----------------------------------------------------------------------------------------------------
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier(kCellIdentifier) as DiaryTableViewCell?
        // cell?.selectionStyle = .None

        println("\(x++)) Inside cell")
        cell!.TitleLabel?.text = "Hello"
        cell!.TitleLabel?.backgroundColor = UIColor.blueColor()
        cell!.SubTitleLabel?.text = "World"
        cell!.contentView.backgroundColor = UIColor.redColor()

        return cell!
    }  
...    
}

我正在接收该单元格,但没有该单元格的元素。

1) Inside cell
(lldb) po cell!.TitleLabel
nil

enter image description here

enter image description here

enter image description here

enter image description here

我清理了代码,编译和编译运行没问题。单元格已加载并涂上红色,以便我可以看到它已加载。但是没有任何单元格的内容被实例化。

为什么?

现在正在看细胞的成员......
但现在我得到了:

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSObject 0x7f9691594810> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key leftImageView.'

如果我断开插座,我会得到图像:

enter image description here

我添加了必需的init()但仍有同样的问题:

class DiaryTableViewCell: UITableViewCell {
    @IBOutlet weak var leftImageView: UIImageView!
    @IBOutlet weak var rightImageView: UIImageView!
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var subTitleLabel: UILabel!

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        NSLog("init coder")
    }
}

3 个答案:

答案 0 :(得分:0)

您已将您的属性声明为隐式解包的选项,并签署您确定它们不是零。因此,不需要使用可选链接。

请使用小写属性名称,因为大写名称用于类名:

class DiaryTableViewCell: UITableViewCell {
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var subTitleLabel: UILabel!
    @IBOutlet weak var leftImageView: UIImageView!
    @IBOutlet weak var rightImageView: UIImageView!
}

然后试试这个:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    if let cell = tableView.dequeueReusableCellWithIdentifier(kCellIdentifier) as? DiaryTableViewCell {
        // cell.selectionStyle = .None

        println("\(x++)) Inside cell")
        cell.titleLabel.text = "Hello"
        cell.titleLabel.backgroundColor = UIColor.blueColor()
        cell.subTitleLabel.text = "World"
        cell.contentView.backgroundColor = UIColor.redColor()

        return cell
    }

    return UITableViewCell()
}  

如果崩溃,那么你的插座绑定肯定有问题。

您是否在InterfaceBuilder中将DiaryTableViewCell设置为nib的类?

答案 1 :(得分:0)

你的tableCellClass缺少nib的awakeFromNib()方法:

class DiaryTableViewCell: UITableViewCell {
    @IBOutlet weak var TitleLabel: UILabel!
    @IBOutlet weak var SubTitleLabel: UILabel!
    @IBOutlet weak var leftImageView: UIImageView!
    @IBOutlet weak var rightImageView: UIImageView!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
}

答案 2 :(得分:0)

我找到了解决方案:
This class is not key value coding-compliant for the key...why?

这是重新发布:

我已将UI元素链接到错误源:

enter image description here

结果如下:
enter image description here