以编程方式实例化TableViewController子类后,其出口为零

时间:2015-08-21 14:17:21

标签: ios swift uitableview null uitextfield

以编程方式实例化EditCountsTableViewCell后,它的出口在访问时返回nil,因此我无法编辑单元格。使用Storyboard实例化时,单元格表现正常。

@IBAction func countNameChanged(sender: AnyObject) {

    let textField = sender as! UITextField
    let text = textField.text

    let superview = textField.superview!
    let cell = superview.superview as! EditCountsTableViewCell
    let indexPath = self.tableView.indexPathForCell(cell)
    let row = indexPath?.row

    var newCell = EditCountsTableViewCell() //Instantiate Cell Programatically

    println(newCell)

    println(newCell.countName) //nil

    if let name = newCell.countName{

        name.text = text

    }

    if let value = newCell.countValue{

        value.text = cell.countValue.text

    }

    tableViewContents[row!] = newCell

}

EditCountsTableViewCell上课:

import UIKit

class EditCountsTableViewCell: UITableViewCell {

    @IBOutlet weak var countName: UITextField!
    @IBOutlet weak var countValue: UITextField!

    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 :(得分:1)

您是否尝试过这种初始化方法?

let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("someViewController") as! UIViewController

(Source)

答案 1 :(得分:0)

您希望以编程方式获取EditCountsTableViewCell变量,但您的单元格是从故事板中提取的,因此您应该从故事板中获取它。