UITableViewCell IBOutlets是零

时间:2015-12-07 02:46:59

标签: ios swift uitableview

我遇到与this非常相似的问题。

我在故事板中有一个自定义的UITableViewCell原型,其中包含图像视图和标签。我控制拖动出口并设置我的单元标识符名称“BasicCell”。

class newsTableViewCell: UITableViewCell {

    @IBOutlet weak var label: UILabel!
    @IBOutlet weak var image1: UIImageView!


    override init(style: UITableViewCellStyle, reuseIdentifier: String!) {
        super.init(style: UITableViewCellStyle.Value1, reuseIdentifier: reuseIdentifier)
    }

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

    func configure(text: String, image2: UIImage){
        label.text = text
        image1.image = image2
    }
}

在我简化的UITableViewController中我有:

class mainlistTableViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.registerClass(newsTableViewCell.classForCoder(), forCellReuseIdentifier: "BasicCell")
    }

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

        let cellIdentifier = "BasicCell";           
        let myCell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! newsTableViewCell
        myCell.configure("12345", image2: UIImage(imageLiteral: "Default.png"))
        return myCell
    }
}

当我这样运行时,我在func configure收到错误,即IBOutlet标签不存在。

当我删除该行:self.tableView.registerClass(newsTableViewCell.classForCoder(), forCellReuseIdentifier: "BasicCell")时,程序在行let myCell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! newsTableViewCell处崩溃并输出以下内容:

 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIImageView _isResizable]: unrecognized selector sent to instance 0x7f9dbb61b360'

关于可能是什么原因的任何想法?

1 个答案:

答案 0 :(得分:1)

故事板中的

选择UITableViewCell原型,在属性检查器集" BasicCell"作为标识符。

右键单击Cell中的UIImageView和UILabel,确保没有未连接或旧连接。

删除self.tableView.registerClass(..)

应该没问题。