我得到了 UITableViewCell,但单元格的元素为零.为什么? [英] I got the UITableViewCell, but cell's elements are nil. Why?

查看:23
本文介绍了我得到了 UITableViewCell,但单元格的元素为零.为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码是经过清洗的&上一篇文章的翻版.
参考:

这里是转载:

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

结果如下:

The following code is a cleansed & rehashed version of a previous post.
ref: 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!
    }  
...    
}

I'm getting the cell, but no elements of that cell.

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

I cleaned up the code, it compiles & runs okay. The cell is loaded and painted with red so I can see it was loaded. But none of the cell's contents are instantiated.

why?

It's seeing the members of the cell now...
But now I'm getting:

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

If I disconnect the outlets, I get the images:

I've added the required init() but still have the same problem:

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")
    }
}

解决方案

I found the solution:
This class is not key value coding-compliant for the key...why?

Here's the repost:

I had linked the UI elements to the WRONG source:

Here's the result:

这篇关于我得到了 UITableViewCell,但单元格的元素为零.为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆