繁体   English   中英

自定义UITableView单元格图像变换不适用于可重复使用的单元格

[英]Custom UITableView cell image transform not applied to reusable cells

我有一个自定义UITableViewCell,它在左侧显示圆形图像。 由于UITableViewCell随附的默认UIImageView与该行的高度相同,因此图像最终几乎会碰到。 我想稍微缩小图像以创建一些额外的填充。

我可以使用以下代码来使其工作

override func layoutSubviews() {
    super.layoutSubviews()

    // Make the image view slightly smaller than the row height
    self.imageView!.transform = CGAffineTransform(scaleX: 0.9, y: 0.9)

    // Round corners
    self.imageView!.layer.cornerRadius = self.imageView!.bounds.height / 2.0
    self.imageView!.layer.borderWidth = 0.5
    self.imageView!.layer.borderColor = UIColor.gray.cgColor
    self.imageView!.layer.masksToBounds = true
    self.imageView!.contentMode = .scaleAspectFill;

    self.updateConstraints()
}

override func prepareForReuse() {
    super.prepareForReuse()
    self.imageView!.image = nil
    self.layoutSubviews()
}

这仅适用于首次出现在屏幕上的表格视图中显示的单元格。 滚动后(即,使可重复使用的单元出队),将不再应用变换。 下图显示了表格视图的左侧。 我已经捕获了原始单元格过渡到重用单元格的区域。

在此处输入图片说明

为了完整性,这是我的tableView(cellForRowAt :)函数

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = self.itemTableView.dequeueReusableCell(withIdentifier: "ItemCell") as! InventoryItemTableViewCell

    if let items = self.displayedItems {
        if indexPath.row < items.count {
            let item = items[indexPath.row]

            cell.item = item
            cell.textLabel!.text = items[indexPath.row].partNumber
            cell.detailTextLabel!.text = items[indexPath.row].description

            if let quantity = items[indexPath.row].quantity {
                cell.quantityLabel.text = "Qty: \(Int(quantity))"
            }
            else {
                cell.quantityLabel.text = "Qty: N/A"
            }

            if let stringImageBase64 = item.imageBase64 {
                let dataDecoded: Data = Data(base64Encoded: stringImageBase64, options: .ignoreUnknownCharacters)!
                cell.imageView!.image = UIImage(data: dataDecoded)
            }
            else {
                cell.imageView!.image = blankImage
            }
        }
    }

    return cell
}

我尝试了其他方法,例如播放图像视图的插图,但这没有效果。

为什么在创建表时将变换应用于原始单元,而不应用于任何重复使用的单元? 我应该采取不同的方法吗?

我能够使用以下代码完成调整图像大小的操作。 似乎在布局后应用了自动布局,因此将其覆盖。

    override func layoutSubviews() {
        // Layout all subviews except for the image view
        super.layoutSubviews()

        // Make the image view slightly smaller than the row height
        self.imageView!.frame = CGRect(x: self.imageView!.frame.origin.x + 4,
                                       y: self.imageView!.frame.origin.y + 4,
                                       width: 56,
                                       height: 56)

        // Round corners
        self.imageView!.layer.cornerRadius = self.imageView!.frame.height / 2.0
        self.imageView!.layer.borderWidth = 0.5
        self.imageView!.layer.borderColor = UIColor.gray.cgColor
        self.imageView!.layer.masksToBounds = false
        self.imageView!.clipsToBounds = true
        self.imageView!.contentMode = .scaleAspectFit;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM