簡體   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