私は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.
制約エラーは何ですか? –
@MilanNosáľ更新。 –