2017-12-22 8 views
0

私はUITableViewCellし、以下のように設定サブビューを作成しよう:ダイナミックな高さを持つテーブルセルの自動レイアウトを正しく設定する方法は?

class MyCellView: UITableViewCell { 

    private let ImageView = UIImageView() 
    private let titleView = UILabel() 

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

     super.init(style: style, reuseIdentifier: reuseIdentifier) 

     contentView.addSubview(ImageView) 
     contentView.addSubview(titleView) 

     ImageView.translatesAutoresizingMaskIntoConstraints = false 
     ImageView.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true 
     ImageView.leftAnchor.constraint(equalTo: contentView.leftAnchor).isActive = true 
     ImageView.rightAnchor.constraint(equalTo: contentView.rightAnchor).isActive = true 
     ImageView.heightAnchor.constraint(equalTo: contentView.widthAnchor, multiplier: 0.6).isActive = true 

     titleView.translatesAutoresizingMaskIntoConstraints = false 
     titleView.topAnchor.constraint(equalTo: ImageView.bottomAnchor).isActive = true 
     titleView.leftAnchor.constraint(equalTo: contentView.leftAnchor).isActive = true 
     titleView.rightAnchor.constraint(equalTo: contentView.rightAnchor).isActive = true 
     titleView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true 

     titleView.numberOfLines = 0 

     titleView.text = " " // <-- *** please note this line 

    } 
} 

そして、テーブルビューに、私は動的な高さを使用するように設定している:私ドン」場合

tableView.estimatedRowHeight = 80 
tableView.rowHeight = UITableViewAutomaticDimension 

私の質問があります

// titleView.text = " " 

私は制約エラーのFRを取得します:T Iは、細胞ビュー内の最後の行を発言等かのように、セル内のtitleViewのテキストを設定しますom XCode。しかし、テキストを設定すると、すべて正常に動作します。

テキストはインターネットから動的にダウンロードされるため、テキストは空である可能性があります。私はテキストを ""空のスペースに設定できるだけでなく、ここで何かを誤解しているのだろうか?私が何かを訂正すれば、テキストを空( ""または無し)にすることができますか?

同様に、titleViewについて完全にコードを削除した場合、セル内にUIImageViewがあるだけで、セルの高さが拡張されていないことがわかりました。どのように私は細胞の動的な高さのイメージを持つことができるようにそれを設定するには?

EDIT:制約エラー:あなたが警告を回避したい場合、約titleView完全にコードを削除したい場合、

class MyCellView: UITableViewCell { 

    private let ImageView = UIImageView() 
    private let titleView = UILabel() 

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

    super.init(style: style, reuseIdentifier: reuseIdentifier) 

    contentView.addSubview(ImageView) 
    contentView.addSubview(titleView) 

    ImageView.translatesAutoresizingMaskIntoConstraints = false 
    ImageView.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true 
    ImageView.leftAnchor.constraint(equalTo: contentView.leftAnchor).isActive = true 
    ImageView.rightAnchor.constraint(equalTo: contentView.rightAnchor).isActive = true 
    let heightConstraint = ImageView.heightAnchor.constraint(equalTo: contentView.widthAnchor, multiplier: 0.6) 
    heightConstraint.priority = UILayoutPriority.defaultLow 
    heightConstraint.isActive = true 

    titleView.translatesAutoresizingMaskIntoConstraints = false 
    titleView.topAnchor.constraint(equalTo: ImageView.bottomAnchor).isActive = true 
    titleView.leftAnchor.constraint(equalTo: contentView.leftAnchor).isActive = true 
    titleView.rightAnchor.constraint(equalTo: contentView.rightAnchor).isActive = true 
    titleView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true 

    titleView.numberOfLines = 0 

// titleView.text = " " 
    } 
} 
  • 以下のコードを試してみてください

    2017-12-22 15:35:34.715865+0800 MyProject[15774:733515] [LayoutConstraints] Unable to simultaneously satisfy constraints. 
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
    (1) look at each constraint and try to figure out which you don't expect; 
    (2) find the code that added the unwanted constraint or constraints and fix it. 
    (
    "<NSLayoutConstraint:0x60c000095450 V:|-(0)-[UIImageView:0x7ff40df0de90] (active, names: '|':UITableViewCellContentView:0x7ff40df0edc0)>", 
    "<NSLayoutConstraint:0x60c0000955e0 UIImageView:0x7ff40df0de90.height == 0.6*UITableViewCellContentView:0x7ff40df0edc0.width (active)>", 
    "<NSLayoutConstraint:0x60c000095630 V:[UIImageView:0x7ff40df0de90]-(0)-[UILabel:0x7ff40df0e0c0] (active)>", 
    "<NSLayoutConstraint:0x60c0000957c0 UILabel:0x7ff40df0e0c0.bottom == UITableViewCellContentView:0x7ff40df0edc0.bottom (active)>", 
    "<NSLayoutConstraint:0x60c000096210 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7ff40df0edc0.height == 248.333 (active)>", 
    "<NSLayoutConstraint:0x60c000095ea0 'UIView-Encapsulated-Layout-Width' UITableViewCellContentView:0x7ff40df0edc0.width == 414 (active)>" 
    ) 
    
    Will attempt to recover by breaking constraint 
    <NSLayoutConstraint:0x60c000095630 V:[UIImageView:0x7ff40df0de90]-(0)-[UILabel:0x7ff40df0e0c0] (active)> 
    
    Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. 
    The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 
    
  • +0

    制約エラーは何ですか? –

    +0

    @MilanNosáľ更新。 –

    答えて

    1
    • セル内に動的高さを持つUIImageViewがあり、imageView

      01にはという制約が追加されています
      class MyCellView: UITableViewCell { 
      
          private let ImageView = UIImageView() 
          private let titleView = UILabel() 
      
          override init(style: UITableViewCellStyle, reuseIdentifier: String?) { 
      
          super.init(style: style, reuseIdentifier: reuseIdentifier) 
      
          contentView.addSubview(ImageView) 
          contentView.addSubview(titleView) 
      
          ImageView.translatesAutoresizingMaskIntoConstraints = false 
          ImageView.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true 
          ImageView.leftAnchor.constraint(equalTo: contentView.leftAnchor).isActive = true 
          ImageView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true 
          ImageView.rightAnchor.constraint(equalTo: contentView.rightAnchor).isActive = true 
          let heightConstraint = ImageView.heightAnchor.constraint(equalTo: contentView.widthAnchor, multiplier: 0.6) 
          heightConstraint.priority = UILayoutPriority.defaultLow 
          heightConstraint.isActive = true 
          } 
      } 
      
    +0

    あなたが示唆したようにコード行を追加しましたが、まだ制約の警告があります。 –

    +0

    @JoeHuang私は私の答えを更新しました、あなたは再び確認することができます。 – trungduc

    2

    知られている「バグ」の結果であると思われる - tableViewによって設定された高さの衝突と自動レイアウトにより算出した高さがあります。セルをレンダリングするとき、最初にtableViewがデフォルトの高さを適用し、自動レイアウトの高さを計算し、後者を使用します。私のquestionを参照してください。

    これは、使用している制約がOK(少なくとも私にとってはそうであるように見える)ことを意味します。高さを定義する制約の1つを優先度= 999に設定するだけです(デフォルトの高さ制約を無効にするまで、矛盾は生じません)。結局、それはあなたの制約をとにかく使用する結果になるので、何の問題も起こりません。

    PS:あなたは(これ、高さ= 0とそれをレンダリング)画像を省略したいと思う場合は、あなただけ、再び(あまりにもトラブルが発生することを、

    ImageView.heightAnchor.constraint(equalTo: contentView.widthAnchor, multiplier: 0.6).isActive = true 
    

    :そのImageViewの高さの制約のために気をつけろ優先順位を下げることで、どこにいてもいいかもしれません)。イメージが常にある場合は、私のP.S.を気にしないでください。 :)。

    関連する問題