1

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.

My image format is PNG.

Here is my code in 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:

class notesTableViewCell: UITableViewCell {

@IBOutlet var buttonImage: UIButton!

    override func awakeFromNib() {
        super.awakeFromNib()
   }
Eliko
  • 1,137
  • 4
  • 16
  • 26
  • Do you actually see the button in the table view if you set a constant background color to it? – arvidurs Oct 04 '17 at 18:06
  • @arvidurs Yes, when I set backgroundColor and also when I just put text. – Eliko Oct 04 '17 at 18:07
  • Whats the size of the png? try setting up 2x 3x resolutions, and default size of 32px by 32px – arvidurs Oct 04 '17 at 18:10
  • Are you sure you are including the imagetest2.png in your target? Just having it in the project isn't enough. – wottle Oct 04 '17 at 19:16
  • Also, malformed pngs can cause this as well. More details can be found here: https://stackoverflow.com/a/11001163/3708242. Of course, I would higly recommend using Image Sets. – wottle Oct 04 '17 at 19:23

2 Answers2

1

Note: Go to Assets.xcassets in your project. then create New Image Set with name imagetest. after that drag and drop imagetest2.png into sample image set

please check button type: .custom

cell.buttonImage.setImage(UIImage(named: "imagetest"), for: .normal)
Yuyutsu
  • 2,509
  • 22
  • 38
  • Well, it worked, but i did a stupid mistake and erased Assets.xcassets. I put it back again and now it doesn't work -_- – Eliko Oct 04 '17 at 18:41
  • Clean project by cmd-option-shift-k. – Yuyutsu Oct 04 '17 at 18:46
  • 1
    This one helped solve the deleting Assets: https://stackoverflow.com/questions/20443306/accidentally-removed-xcassets-file-from-xcode-project – Eliko Oct 04 '17 at 18:55
  • But, why does this work, and my code didn't? everywhere around the web I found codes like I did and it worked for them. – Eliko Oct 04 '17 at 18:56
0

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.

enter image description here

Hope it will work.

Abhishek Mitra
  • 3,335
  • 4
  • 25
  • 46