簡體   English   中英

UITableViewCell中的更新約束不正確

[英]Updating Constraints in UITableViewCell are not properly

我想在圖像高度的情況下在UITableView中創建一個動態單元格。 一切正常,但是當我滾動UITableView ,調試器大喊大叫我單元格中的不良約束,但是單元格正是我想要的。 當我看到此調試信息時,我不能保持冷靜;)

我的CustomCell類如下所示:

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單元格看起來像這樣:

在此處輸入圖片說明

TitleLabel沒有高度限制。 僅前導,頂部,尾隨,底部。

我的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
}

}

在模擬器中的外觀:

在此處輸入圖片說明

每件事看起來都不錯,但是控制台給了我這種錯誤:

在此處輸入圖片說明

不知道為什么 我試圖用layoutIfNeeded() updateConstraints()setNeedsLayout()didSetCustomCell ,但於事無補。 有什么建議么?

我想您在圖像上有一些多余的限制。 它們可能是高度/底部/頂部約束。 一個應該被刪除

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM