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()
}
Note: Go to
Assets.xcassets
in your project. then create New Image Set with nameimagetest
. after that drag and dropimagetest2.png
into sample image set
please check button type:.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.
Hope it will work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.