2016-09-14 8 views
0

私の自動制約で何が起こっているのかわからないので、私はあなたの助けが必要です。私のスイッチの制約は、アプリケーションをクラッシュさせる。私はそれらを削除するとうまく動作します。 これは私が受け取るエラーメッセージです: '|'を解釈できません関連するビューはスーパービューを持たないため、文字: H:| -100- [v0(35)] |'|'を解釈できません文字

class selectionCustomCell: UITableViewCell{ 
    var label: UILabel = { 
     let attribution = UILabel() 
     attribution.text = "Nom du label" 
     attribution.textColor = UIColor(r: 0, g: 185, b: 255) 
     attribution.lineBreakMode = NSLineBreakMode.ByWordWrapping 
     attribution.numberOfLines = 0 
     attribution.translatesAutoresizingMaskIntoConstraints = false 
     return attribution 
    }() 

    var switchElement: UISwitch{ 
     let sL = UISwitch() 
     sL.setOn(true, animated: true) 
     sL.onTintColor = UIColor(r: 0, g: 185, b: 255) 
     sL.tintColor = UIColor(r: 0, g: 185, b: 255) 
     sL.translatesAutoresizingMaskIntoConstraints = false 
     return sL 
    } 

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) { 
     super.init(style: .Default, reuseIdentifier: reuseIdentifier) 
     addSubview(switchElement) 
     addSubview(label) 
     setupViews() 
    } 

    func setupViews(){ 
     addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-20-[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": label]))   
     addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-20-[v0]", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": label])) 


     addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-100-[v0(35)]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": switchElement])) 


     addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-20-[v0(35)]", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": switchElement])) 


    } 

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

} 

答えて

6

注宣言されているかlabelswitchViewの違い:labelが最初に実行されたクロージャの出力に初期化されているあなたの助けここ

ため

THXは、私のコードですそれが参照される時間。 switchViewは、参照されるたびに呼び出されるゲッター付きの計算プロパティーです。つまり、-setupViewsで参照しているバージョンは、以前に-addSubviewと呼ばれていたものと同じではありません。ビュー階層に属さないため、ビジュアル形式は無効です。

あなたがswitchView試合の宣言labelの宣言を行った場合、予想通り、あなたのコードが動作するはずです:あなたは非常に多くの

var switchElement: UISwitch = { // note the equal operator here 
    let sL = UISwitch() 
    // ... 
    return sL 
}() // note the invocation of the block here 
+0

Thxを。できます !!!私は私の髪を引っ張っていた –

関連する問題