2017-04-24 18 views
0

メッセンジャーアプリでメッセージの隣に日付/時刻を追加しようとしています。現在のところ、セルの自動サイズは、メッセージに最適です。しかし、セルに2番目のラベルを追加しようとすると、キャッチされない例外で終了するランタイムエラーが発生します。現在のメッセージテキストがセルの右側にあるセルの左側に日付を作成しようとしています。制約を削除すると、timeLabelは決して表示されません。ここで2 UITableViewのカスタムセルのラベル

はここ

 cell.myLabel.font = font 
     cell.myLabel.numberOfLines = 0 
     cell.myLabel.text = self.messages[indexPath.row] 

     cell.myLabel.textColor = UIColor.blue 
     cell.myLabel.textAlignment = .justified 

     let marginGuide = cell.contentView.layoutMarginsGuide 
     cell.myLabel.translatesAutoresizingMaskIntoConstraints = false 
     cell.myLabel.bottomAnchor.constraint(equalTo: marginGuide.bottomAnchor).isActive = true 
     cell.myLabel.topAnchor.constraint(equalTo: marginGuide.topAnchor).isActive = true 

     cell.myLabel.trailingAnchor.constraint(equalTo: marginGuide.trailingAnchor, constant: 0).isActive = true 
     cell.myLabel.backgroundColor = UIColor(patternImage: UIImage(named: "background")!) 

     if size1.width > 260 { 
      cell.myLabel.preferredMaxLayoutWidth = 260 
     } 
     else{ 
      cell.myLabel.preferredMaxLayoutWidth = size1.width 
     } 
     let interval = self.timeStamps[indexPath.row] 
     if let myDouble = NumberFormatter().number(from: interval)?.doubleValue { 
      let date = NSDate(timeIntervalSince1970: myDouble) 
      cell.timeLabel.font = font 
      cell.timeLabel.text = "date"// String(describing: date) 
      cell.timeLabel.translatesAutoresizingMaskIntoConstraints = false 

      cell.timeLabel.bottomAnchor.constraint(equalTo: marginGuide.bottomAnchor).isActive = true 
      cell.timeLabel.topAnchor.constraint(equalTo: marginGuide.topAnchor).isActive = true 
      cell.timeLabel.leadingAnchor.constraint(equalTo: marginGuide.leadingAnchor).isActive = true 
     } 

cellforRowatIndexPath方法のほとんどで、私が作成したカスタムセルのための全体のプログラムです。

import UIKit 

class messageTableViewCell: UITableViewCell { 

var myLabel = UILabel() 
var background = UIImageView() 
var timeLabel = UILabel() 

override init(style: UITableViewCellStyle, reuseIdentifier: String?) { 
    super.init(style: style, reuseIdentifier: reuseIdentifier) 

    self.contentView.addSubview(background) 
    self.contentView.addSubview(myLabel) 
} 

required init?(coder aDecoder: NSCoder) { 
    fatalError("init(coder:) has not been implemented") 
} 

override func layoutSubviews() { 
    super.layoutSubviews() 
    myLabel.numberOfLines = 0 
    myLabel.lineBreakMode = NSLineBreakMode.byWordWrapping 
    myLabel.sizeToFit() 
    let marginGuide = contentView.layoutMarginsGuide 
    myLabel.translatesAutoresizingMaskIntoConstraints = false 
    myLabel.bottomAnchor.constraint(equalTo: marginGuide.bottomAnchor).isActive = true 
    myLabel.topAnchor.constraint(equalTo: marginGuide.topAnchor).isActive = true 
    myLabel.trailingAnchor.constraint(equalTo: marginGuide.trailingAnchor, constant: 0).isActive = true 
    myLabel.backgroundColor = UIColor(patternImage: UIImage(named: "background")!) 

    timeLabel.numberOfLines = 0 
    timeLabel.lineBreakMode = NSLineBreakMode.byWordWrapping 
    timeLabel.sizeToFit() 



} 

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

クラッシュを引き起こしているコード行はありますか? – nathan

+0

timeLabelの制約を設定する行: cell.timeLabel.bottomAnchor.constraint(equalTo:marginGuide.bottomAnchor).isActive = true – lawndogg

+0

そして、クラッシュに関するコンソールの情報はありますか?あなたはその理由を知っていますか? – nathan

答えて

0

私は不注意に間違いを犯しました。セルクラスにサブビューを追加するのを忘れました。 私はこの行が必要でした:

self.contentView.addSubview(timeLabel) 
関連する問題