2017-06-20 2 views
1

UITableViewAutomaticDimensionの行の高さとセルの自動レイアウトの組み合わせを使用して拡張可能なセルを作成しようとしています。物事をシンプルにするために、私は私が仕事に期待することを、次の簡単なコードで開始:拡張可能なUITableViewセルがUIViewAlertForUnsatisfiableConstraintsの結果を返します

import UIKit 

class ViewController: UITableViewController { 

    let cells = [Cell(), Cell(), Cell()] 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     tableView.rowHeight = UITableViewAutomaticDimension 
     tableView.estimatedRowHeight = 50 

     for cell in cells { 
      cell.tableView = tableView 
     } 
    } 

    override func numberOfSections(in tableView: UITableView) -> Int { 
     return 1 
    } 

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return 3 
    } 

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     return cells[indexPath.row] 
    } 
} 

class Cell: UITableViewCell { 

    var cons: NSLayoutConstraint? 
    var tableView: UITableView? 

    init() { 
     super.init(style: .default, reuseIdentifier: nil) 

     let button = UIButton() 
     contentView.addSubview(button) 
     button.addTarget(self, action: #selector(Cell.tapped(_:)), for: .touchUpInside) 
     button.setTitle("Press me", for: .normal) 
     button.setTitleColor(.red, for: .normal) 

     button.translatesAutoresizingMaskIntoConstraints = false 

     // constraining the button to the view, so that buttons height would define cell height 
     let constraints = [ 
      button.topAnchor.constraint(equalTo: contentView.topAnchor), 
      button.rightAnchor.constraint(equalTo: contentView.rightAnchor), 
      button.bottomAnchor.constraint(equalTo: contentView.bottomAnchor), 
      button.leftAnchor.constraint(equalTo: contentView.leftAnchor), 
     ] 

     NSLayoutConstraint.activate(constraints) 
     cons = button.heightAnchor.constraint(equalToConstant: 100) 
     cons?.isActive = true 
    } 

    func tapped(_ sender: UIButton) { 
     // change the height of the button, thus of the contentView too (since the button is constrained to the contentView) 
     if cons?.constant == 100 { 
      cons?.constant = 200 
     } else { 
      cons?.constant = 100 
     } 
     // tell the tableView to redraw itself to apply the change 
     tableView?.beginUpdates() 
     tableView?.setNeedsDisplay() 
     tableView?.endUpdates() 
    } 

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

さて、これは動作しているようだ、しかし、私はまだ、次のメッセージとUIViewAlertForUnsatisfiableConstraints警告取得操作を行います。

(
"<NSLayoutConstraint:0x17408c7b0 V:|-(0)-[UIButton:0x109e10290'Press me'] (active, names: '|':UITableViewCellContentView:0x109e0fd90)>", 
"<NSLayoutConstraint:0x17408c8a0 UIButton:0x109e10290'Press me'.bottom == UITableViewCellContentView:0x109e0fd90.bottom (active)>", 
"<NSLayoutConstraint:0x17408c990 UIButton:0x109e10290'Press me'.height == 200 (active)>", 
"<NSLayoutConstraint:0x17408db10 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x109e0fd90.height == 100 (active)>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x17408c990 UIButton:0x109e10290'Press me'.height == 200 (active)> 

推定された(または現在の)行の高さが私の提供する自動レイアウト制約と衝突しているようです。それでも、次のコードを使用してtableViewをリフレッシュすると、期待通りに表示されます:

tableView?.beginUpdates() 
tableView?.setNeedsDisplay() 
tableView?.endUpdates() 

警告を削除するにはどうすればよいですか?

高さ制約の優先度を999に変更するだけで警告が削除されることはわかっていますが、解決策としてではなく、削除するためのハックと思われます。私が間違っている場合は、説明してください。ありがとう。

+0

変更prority –

答えて

1

はハックのようなを感じることがあり999に高さ制約の優先順位を設定するが、Appleのドキュメントによると:使用する義務は感じていない

NOTEすべて1000の優先順位値。実際、優先順位は、システム定義の低(250)、中(500)、高(750)、および必要(1000)の優先順位を中心に一般的に集中する必要があります。タイを防ぐために、これらの値より1つまたは2つ上または下の制約を設定する必要があります。それをはるかに超えている場合は、おそらくレイアウトのロジックを再検討したいと考えています。直感的なよりもおそらく少し小さい

「私はセルの高さを制御するためのボタンの高さが欲しいので、その優先順位????」

しかし、それはそれを行うための "正しい"方法と思われます。

文献:高さの制約の下にhttps://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/AnatomyofaConstraint.html#//apple_ref/doc/uid/TP40010853-CH9-SW19

-2

上限制約を削除します。つまり、上限制約と下限制約が競合するため200を削除します。

はそれがお役に立てば幸いです。..

+0

高さ制約は、セルの膨張/崩壊高さを定義します。それはそこになければならない。 –

関連する問題