Ios UITableView在初始荷载下未正确绘制单元格-Swift

Ios UITableView在初始荷载下未正确绘制单元格-Swift,ios,swift,uitableview,Ios,Swift,Uitableview,我有一个nib文件中的表视图,它可以正确加载数据。它加载所需数量的单元格并正确填充数据。该单元也是一个包含多个视图的xib文件。每个单元格都有一个自定义高度。我使用以下方法设置了每个单元格的正确高度: func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return (data[indexPath.row].height + 200) } 问题在于,在

我有一个nib文件中的表视图,它可以正确加载数据。它加载所需数量的单元格并正确填充数据。该单元也是一个包含多个视图的xib文件。每个单元格都有一个自定义高度。我使用以下方法设置了每个单元格的正确高度:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return (data[indexPath.row].height + 200)
}
问题在于,在初始荷载下,传感器无法正确绘制,如下所示:

描述区域不会移动到我通过更改其X和Y坐标使其移动的位置

但是,当我将表格视图滚动到最初加载的单元格之外并返回到这些单元格时,这一问题得到了解决,如下所示:

加载单元格后,我移动图像下方的描述区域。这适用于单元格,但不适用于最初加载的单元格。它只有在我将被破坏的单元格从视图中滚动出来并返回到它们时才会得到修复。我该如何解决这个问题?我怎样才能使电池在初始负载时得到正确的拉伸

编辑:澄清:单元格加载正确,但绘制不正确。我必须从最初的几个单元格中滚动出来,然后再滚动回来,这样才能正确绘制单元格


感谢您的帮助。谢谢大家!

我可以看到您尝试加载的图像的尺寸比图像容器视图的尺寸大。 您可以尝试为imageView指定clipsToBound=True

这很可能会解决您的问题。 谢谢。

您想根据其中的图像增加UITableViewCell的高度,对吗

下面是tableview数据源和委托

扩展名MyDataSource:UITableViewDelegate,UITableViewDataSource{

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if let height = self.rowHeights[indexPath.row] {
        return height
    } else {
        return 100
    }
}

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    if let height = self.rowHeights[indexPath.row] {
        return height
    } else {
        return 100
    }
}

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let mydata = mydatas[indexPath.row]

    var cell: UITableViewCell
    cell = tableView.dequeueReusableCell(withIdentifier: 'myCell.cellIdentify()')!
    cell.contentView.backgroundColor = UIColor.clear
    cell.backgroundColor = UIColor.clear
    cell.tag = indexPath.row
    cell.selectionStyle = .none

    let image = UIImage(named: promotion)
    (cell as! myCell).configureCell(downloadImage: image!)
    let aspectRatio = (image?.size.height)!/(image?.size.width)!
    let imageHeight = (cell.contentView.frame.width * aspectRatio) + 16

    UIView.performWithoutAnimation {
        self.myTableView?.beginUpdates()
        self.rowHeights[indexPath.row] = imageHeight
        self.myTableView?.endUpdates()
    }

    return cell
}
}

下面是UITableViewCell自定义类

迈塞尔


什么是数据?我用作表数据源的字典变量。它的图像有一个height属性,因此我可以设置表行的高度。是的,图像比容器视图大。我尝试添加clipsToBound,但同样的事情仍然发生。谢谢,虽然我已经成功地根据内容更改了单元格高度。我想做的是正确地绘制最初加载的单元格。非常感谢。though@Chris最初加载的单元格无法正确绘制,因为加载前我们无法知道加载图像的分辨率。但您可以在UITableView中设置estimaterowheight。
import UIKit

class myCell: UITableViewCell {

    @IBOutlet weak var imageHolder: WMView!
    @IBOutlet weak var actionImage: UIImageView!
    @IBOutlet weak var loaderIndicator: UIActivityIndicatorView!

    override func prepareForReuse() {
        super.prepareForReuse()
        self.loaderIndicator.isHidden = false
        self.loaderIndicator.startAnimating()
        self.actionImage.image = nil
    }

    static func cellHeight() -> CGFloat {
        return 73.0
    }

    static func cellIdentify() ->String {
        return "myCell"
    }

    override func awakeFromNib() {
        super.awakeFromNib()
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }

    func configureCell(downloadImage: UIImage) {
        self.imageHolder.layer.borderWidth = 1
        self.imageHolder.layer.cornerRadius = 5
        self.imageHolder.layer.borderColor = UIColor.clear.cgColor

        self.imageHolder.layer.masksToBounds = true
        self.imageHolder.clipsToBounds = true

        //ImageHolder
        self.actionImage.image = downloadImage
        self.loaderIndicator.isHidden = true
    }
}