UILabel连接到插座,但是没有

时间:2015-07-14 15:25:56

标签: ios uitableview uiscrollview visual-format-language

我正在尝试创建一个UIScrollView包含UIImageViewerUIViewUITableView的屏幕。对于UITableView,我有一个自定义UITableViewCell,其中包含两个UILabels。问题是,UITableView的数据源方法tableView(tableView, cellForRowAtIndexPath: indexPath)-> UITableViewCell,在我ItemTableCell出列后,我无法设置单元格UILabels的文本,因为由于某种原因,他们是零。

以下是我viewDidLoad()的代码,它非常冗长:

@IBOutlet weak var descriptionLabel: UILabel!
@IBOutlet weak var scrollView: UIScrollView!

var itemSelected: Item!
var tableView = UITableView()
var imageViewer: UIImageView!
var firstDetailView: UIView!

let detailCell = "ItemDetailCell"

override func viewDidLoad() {
    super.viewDidLoad()

    descriptionLabel.text = itemSelected.description.capitalizedString

    //setting delegates and data sources
    scrollView.delegate = self
    tableView.dataSource = self
    tableView.delegate = self

    //registering cell class
    tableView.registerClass(ItemDetailCell.self, forCellReuseIdentifier: detailCell)

    //setting up imageViewer
    imageViewer = UIImageView(frame: CGRectMake(0, 0, view.frame.width, view.frame.height / 2.0))
    imageViewer.image = itemSelected.images[1]
    imageViewer.contentMode = .ScaleAspectFill
    imageViewer.clipsToBounds = true

    //setting up firstdetailview
    firstDetailView = UIView(frame: CGRectMake(0.0, imageViewer.frame.height, view.frame.width, 50))
    firstDetailView.layer.borderColor = UIColor.grayColor().CGColor
    firstDetailView.layer.borderWidth = 0.5

    //disabling autoresizing for tableview, firstdetailview, and imageviewer
    tableView.setTranslatesAutoresizingMaskIntoConstraints(false)
    firstDetailView.setTranslatesAutoresizingMaskIntoConstraints(false)
    imageViewer.setTranslatesAutoresizingMaskIntoConstraints(false)

    // setting up itemlabel
    var itemLabel = UILabel()
    itemLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
    itemLabel.font = UIFont(name: "HelveticaNeue-Light", size: 14.0) ?? UIFont()
    itemLabel.text = itemSelected.title
    firstDetailView.addSubview(itemLabel)

    //setting up pricelabel
    var priceLabel = UILabel()
    priceLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
    priceLabel.font = UIFont(name: "Helvetica-Bold", size: 14.0) ?? UIFont()
    priceLabel.text = "$" + String(format:"%.2f", itemSelected.price)
    priceLabel.textAlignment = .Right
    firstDetailView.addSubview(priceLabel)

    //adding horizontal constraints to itemLabel and pricelabel
    let views = Dictionary(dictionaryLiteral: ("itemLabel",itemLabel),("priceLabel",priceLabel))
    let hConstraint = NSLayoutConstraint.constraintsWithVisualFormat("H:|-8-[itemLabel]-[priceLabel]-8-|", options: nil, metrics: nil, views: views)
    firstDetailView.addConstraints(hConstraint)

    //adding vertical constrains to itemlabel and pricelabel
    let itemVContraints = NSLayoutConstraint.constraintsWithVisualFormat("V:|[itemLabel]|", options: nil, metrics: nil, views: views)
    firstDetailView.addConstraints(itemVContraints)
    let priceVConstraints = NSLayoutConstraint.constraintsWithVisualFormat("V:|[priceLabel]|", options: nil, metrics: nil, views: views)
    firstDetailView.addConstraints(priceVConstraints)

    //adding subviews to scrollView
    scrollView.addSubview(imageViewer)
    scrollView.addSubview(firstDetailView)
    scrollView.addSubview(tableView)

    //master views dictionary
    let masterViews = Dictionary(dictionaryLiteral: ("tableView",tableView),("firstDetailView",firstDetailView),("imageViewer",imageViewer),("scrollView",scrollView))

    //setting up horizontal and vertical constrains for imageviewer, firstdetailview, and tableView
    let hTblConstraints = NSLayoutConstraint.constraintsWithVisualFormat("H:|[tableView(==scrollView)]|", options: nil, metrics: nil, views: masterViews)
    let hImgConstrains = NSLayoutConstraint.constraintsWithVisualFormat("H:|[imageViewer(==scrollView)]|", options: nil, metrics: nil, views: masterViews)
    let hFrstDtlConstraints = NSLayoutConstraint.constraintsWithVisualFormat("H:|[firstDetailView(==scrollView)]|", options: nil, metrics: nil, views: masterViews)
    let vConstraints = NSLayoutConstraint.constraintsWithVisualFormat("V:|[imageViewer(==300)]-0-[firstDetailView(==50)]-0-[tableView(==400)]|", options: nil, metrics: nil, views: masterViews)

    //adding constraints to scrollView
    scrollView.addConstraints(hTblConstraints)
    scrollView.addConstraints(hImgConstrains)
    scrollView.addConstraints(hFrstDtlConstraints)
    scrollView.addConstraints(vConstraints)

}

以下是UITableView数据源和委托方法的代码:

// MARK: - Table View Delegate Methods

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return CGFloat(50)
}

// MARK: - Table View Data Source Methods

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 4
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier(detailCell) as! ItemDetailCell

    cell.detailTitle = "Detail:"
    cell.detail = "Some detail."

    return cell
}

现在,问题实际上在哪里,我ItemDetailCell的代码:

class ItemDetailCell: UITableViewCell {

@IBOutlet weak var detailTitleLabel: UILabel!
@IBOutlet weak var detailLabel: UILabel!

var detailTitle: String {
    get {
        return detailTitleLabel.text!
    }
    set(newTitle){
        detailTitleLabel.text = newTitle
    }
}

var detail: String {
    get {
        return detailLabel.text!
    }
    set(newDetail) {
        detailLabel.text = newDetail
    }
}

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
}

}

当我在模拟器中运行代码时,fatal error: unexpectedly found nil while unwrapping an Optional value set(newTitle)detailTitledetailTitleLabeldetailLabelnilaerobic-forge-504

可能是什么问题?

1 个答案:

答案 0 :(得分:1)

我实际上知道自己做错了什么。抱歉这么长的问题!

我需要在这些方面添加一些内容:

let nib = UINib(nibName: "nameOfNib", bundle: nil)
tableView.registerNib(nib, forCellReuseIdentifier: cellTableIdentifier)