2017-10-04 71 views
1

我在我的tableView中有一個UIButton,我試圖改變它的圖像,但是當我運行該應用程序時 - 在模擬器和iPhone中,它什麼也沒有顯示。在tableview中看不到按鈕的圖像單元格

我的圖片格式是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() 
    } 
+0

你真的看到表視圖按鈕,如果你設定一個固定的背景顏色呢? – arvidurs

+0

@arvidurs是的,當我設置backgroundColor和當我只是把文本。 – Eliko

+0

png的大小是多少?嘗試設置2x 3x分辨率,默認大小爲32像素×32像素 – arvidurs

回答

1

注意:在項目中轉到Assets.xcassets。然後創建名稱爲imagetest的新圖像集。後拖放到imagetest2.png樣本圖像設定

請按鈕類型:.custom

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

嗯,它的工作,但我做了一個愚蠢的錯誤,並刪除Assets.xcassets。我再次把它放回去,現在它不起作用-_- – Eliko

+0

通過cmd-option-shift-k清理項目。 – Yuyutsu

+1

這一個幫助解決刪除資產:https://stackoverflow.com/questions/20443306/accidentally-removed-xcassets-file-from-xcode-project – Eliko

0

你可以試試這個:

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

另外,儘量沒有內容模式。一旦

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

UPDATE

好吧,轉到您的tableviewcell,選擇按鈕,並在屬性部分,選擇類型的自定義。

enter image description here

希望它會工作。

+0

不幸的是,它們都不起作用 – Eliko

+0

你有這個名字的圖像「imagetest2.png」嗎? –

+0

是的,當然.. – Eliko

相關問題