簡體   English   中英

在tableView Cell中看不到按鈕的圖像

[英]Can't see image of button in tableView Cell

我的tableView有一個UIButton ,我試圖更改它的圖像,但是當我運行該應用程序時 - 在模擬器和 iPhone 中,它什么也沒顯示。

我的圖像格式是PNG

這是我在cellForRowAt代碼:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! notesTableViewCell
        cell.buttonImage.setImage(UIImage(named: "imagetest2.png"), for: UIControlState.normal)
        cell.buttonImage.imageView?.contentMode = .scaleAspectFit
        cell.buttonImage.tag = indexPath.row
        cell.buttonImage.addTarget(self, action: #selector(buttonImageFunc), for: .touchUpInside)
        cell.selectionStyle = UITableViewCellSelectionStyle.none

        return cell
    }

這是notesTableViewCell代碼:

class notesTableViewCell: UITableViewCell {

@IBOutlet var buttonImage: UIButton!

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

注意:轉到項目中的Assets.xcassets 然后創建名為imagetest新圖像集。 然后將imagetest2.png拖放到示例圖像集中

請檢查按鈕類型: .custom

cell.buttonImage.setImage(UIImage(named: "imagetest"), for: .normal)

你可以試試這個:

cell.buttonImage.setImage(UIImage(named: "imagetest2.png"), for: .normal)
cell.buttonImage.imageView?.contentMode = .scaleAspectFill

另外,嘗試不使用內容模式。 一次

//cell.buttonImage.imageView?.contentMode = .scaleAspectFill

更新

好的,轉到您的 tableviewcell,選擇您的按鈕,然后在屬性部分中,選擇 type custom。

在此處輸入圖片說明

希望它會起作用。

暫無
暫無

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

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