2017-08-15 6 views
0

画像の高さの場合、UITableViewに動的セルを作成したいと思います。すべて正常に動作しますが、私がUITableViewをスクロールすると、デバッガはセルの悪い制約について私に叫んでいますが、セルは正確に私が望むものに見えます。UITableViewCellの制約が正しく更新されていません

class CustomCell: UITableViewCell { 

//MARK: - Outlets 

@IBOutlet weak var photoImageView: UIImageView! 
@IBOutlet weak var titleLabel: UILabel! 
@IBOutlet weak var heightConstraint: NSLayoutConstraint! 


//MARK: - Variables 

var dream: MyDream? { 
    didSet { 
     if let item = dream, let image = UIImage(data: item.photoData) { 
      scaleImageView(image: image) 
      photoImageView.image = image 
      titleLabel.text = item.title 
     } 
    } 
} 

func scaleImageView(image: UIImage) { 
    let imageWidth = image.size.width 
    let imageScale = image.size.height/image.size.width 
    if imageWidth > photoImageView.bounds.width { 
     heightConstraint.constant = photoImageView.bounds.size.width * imageScale 
    } else { 
     heightConstraint.constant = imageWidth * imageScale 
    } 
} 
} 

.xibの私の細胞がそのようになっています:

enter image description here

TitleLabel doesn't have a height constraint. Leading, Top, Trailing, Bottom only.

マイCustomCellクラスはそのように見える);私はこのデバッグ情報を見るとき、私は冷静にできません

と私のViewControllerは非常に単純で、次のようになります。

それは、シミュレータでどのように見えるか
class ViewController: UIViewController { 

//MARK: - Outlets 

@IBOutlet var tableView: UITableView! 

//MARK: - Variables 

lazy var dreams = [MyDream]() 


override func viewDidLoad() { 
    super.viewDidLoad() 
    createModel() // I mock data here :) 
    setTableView() 
} 

func setTableView() { 
    tableView.delegate = self 
    tableView.dataSource = self 
    tableView.estimatedRowHeight = 100 
    tableView.rowHeight = UITableViewAutomaticDimension 
    tableView.register(UINib(nibName: String(describing: CustomCell.self), bundle: nil), forCellReuseIdentifier: "cell") 
} 

func createModel() { 
    for i in 0..<12 { 
     if i < 6 { 
      if let image = UIImage(named: "\(i + 1)"), let imageData = UIImageJPEGRepresentation(image, 100) { 
       let dream = MyDream(photoData: imageData, title: "Image number \(i + 1)") 
       dreams.append(dream) 
      } 
     } else { 
      if let image = UIImage(named: "\(i - 5)"), let imageData = UIImageJPEGRepresentation(image, 100) { 
       let dream = MyDream(photoData: imageData, title: "Image number \(i + 1)") 
       dreams.append(dream) 
      } 
     } 
    } 
} 

} 

extension ViewController: UITableViewDelegate, UITableViewDataSource { 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return dreams.count 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomCell 
    cell.dream = dreams[indexPath.row] 
    cell.selectionStyle = .none 
    return cell 
} 

} 

enter image description here

すべてのもの罰金に見えますが、コンソールは私にこの種のエラーを与える:

enter image description here

私はなぜ知りません?私はlayoutIfNeeded(),updateConstraints()setNeedsLayout()didSetCustomCellで使用しようとしましたが、助けになりません。助言がありますか?

+0

テーブルビュー内の行の推定高さを設定したか、または 'estimatedHeightForRow(at:)'関数を実装しましたか? – Paulw11

+0

彼は 'estimatedRowHeight'を設定し、' UITableViewAutomaticDimension'の 'rowHeight'を必要としました。 「カプセル化されたレイアウトの高さ」に関するこのような警告は、縦方向の制約の1つを1000から999に下げることで解決できますが、これによりエラーは防げますが、高さは正しく計算されます。例えば。 https://stackoverflow.com/q/25059443/1271826 SOに 'UIView-Encapsulated-Layout-Height'を検索すると、このトピックに関する多くの議論が見られます。 – Rob

答えて

0

私はあなたのイメージにいくつかの冗長な制約があると思います。 高さ/下限/上の制約になります。 1つを削除する必要があります

+0

いいえ、エラーが 'UIView-Encapsulated-Layout-Height'に関連しているということは、これが問題ではないことを示唆しています。動的にサイジングする行のよく知られた、厄介な "機能"です。 – Rob

関連する問題