简体   繁体   English

在tableView Cell中看不到按钮的图像

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

I have a UIButton in my tableView , I am trying to change it's image, but when I run the app - in the simulator and in the iPhone, it shows nothing.我的tableView有一个UIButton ,我试图更改它的图像,但是当我运行该应用程序时 - 在模拟器和 iPhone 中,它什么也没显示。

My image format is PNG .我的图像格式是PNG

Here is my code in cellForRowAt :这是我在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
    }

This is notesTableViewCell code:这是notesTableViewCell代码:

class notesTableViewCell: UITableViewCell {

@IBOutlet var buttonImage: UIButton!

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

Note: Go to Assets.xcassets in your project.注意:转到项目中的Assets.xcassets then create New Image Set with name imagetest .然后创建名为imagetest新图像集。 after that drag and drop imagetest2.png into sample image set然后将imagetest2.png拖放到示例图像集中

please check button type: .custom请检查按钮类型: .custom

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

You can try this:你可以试试这个:

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

Also, try without content mode.另外,尝试不使用内容模式。 Once一次

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

UPDATE更新

Ok, Go to your tableviewcell, select your button and in property section, select type custom.好的,转到您的 tableviewcell,选择您的按钮,然后在属性部分中,选择 type custom。

在此处输入图片说明

Hope it will work.希望它会起作用。

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

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