繁体   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