2017-04-24 18 views
0

메신저 앱에서 메시지 옆에 날짜/시간을 추가하려고합니다. 현재 셀의 자동 크기는 메시지에 적합합니다. 그러나 셀에 두 번째 레이블을 추가하려고하면 잡히지 않은 예외로 종료하는 런타임 오류가 발생합니다. 현재 메시지 텍스트가 셀의 오른쪽에있는 셀의 왼쪽에 날짜를 만들려고합니다. 제약 조건을 삭제하면 timeLabel이 표시되지 않습니다. 여기2 UITableView에서 사용자 정의 셀의 레이블

는 cellforRowatIndexPath 방법 여기

 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 
     } 

의 대부분은 내가 만든 사용자 지정 셀의 전체 프로그램입니다.

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 
} 

} 

누군가이 두 번째 레이블을 추가하는 방법을 알려주세요. 나는 모든 것을 시도했다.

+0

어떤 코드가 충돌을 일으키고 있습니까? – nathan

+0

timeLabel에 대한 제약 조건을 설정하는 줄 : cell.timeLabel.bottomAnchor.constraint (equalTo : marginGuide.bottomAnchor) .isActive = true – lawndogg

+0

그리고 충돌에 대한 콘솔의 정보를 얻습니까? 이유가 무엇인지 아십니까? – nathan

답변

0

부주의 한 실수를 저질렀습니다. 셀 분류에 하위보기를 추가하는 것을 잊었습니다. 다음 라인이 필요합니다 :

self.contentView.addSubview(timeLabel)