2016-01-05 50 views
3

我試圖在選中它時更改單元格內的圖像。以下是我在我的視圖控制器:在桌面單元格內選擇更改圖像 - Swift

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 

let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomTableViewCell 

     cell.bgImage.image = UIImage(named: "second_image") 

} 

這是我設置的圖像:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomTableViewCell 

cell.bgImage.image = UIImage(named: "first_image") 

} 

當我選擇單元格,圖像不更新。我如何得到這個工作?

+0

謝謝!但這不會更新圖像:( –

+0

對不起,這給出了一個錯誤,說該單元沒有名爲bgImage –

+1

的成員而且你的imageView被稱爲bgImage?你可以發佈你的代碼添加imageView到單元格的位置 –

回答

9

該問題可能是您撥打dequeueReusableCellWithIdentifier。嘗試使用cellForRowAtIndexPath代替(documentation about it here)。這將爲您提供對當前單元格的引用,而不是對新的可重用單元格的引用。

因此,根據上述代碼:

let cell = self.tableView.cellForRowAtIndexPath(indexPath) as! CustomTableViewCell

這類型轉換爲關鍵因爲cellForRowAtIndexPath回報UITableViewCell,這不會有bgImage成員。

此外,請確保您有一個成員加入@IBOutlet strong var bgImage: UIImageView!稱爲bgImage這是您的CustomTableViewCellUIImageView,並且所述IBOutlet連接在你的情節提要/廈門國際銀行。

3

更改didSelectRowAtIndexPath到:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    let cell = tableView.cellForRowAtIndexPath(indexPath) 
    cell.bgImage.image = UIImage(named: "second_image") 
} 

按照默認這部作品改變形象

let cell = tableView.cellForRowAtIndexPath(indexPath) 
cell!.imageView?.image = UIImage(named: "2") 
+0

對不起,這給出了一個錯誤,說該單元格沒有成員名爲bgImage –

+1

作爲默認'cell!.imageView?.image = UIImage(名爲:「2」)'這工作更新圖像的cell,但是你已經擴展了你的單元並調用了'bgImage'因此你需要檢查你的'CustomTableViewCell'。否則,如果您願意,我可以爲您提供我嘗試的示例(更改圖像的默認方式)。 –

+0

這可能與它有關嗎? http://stackoverflow.com/questions/13844724/cant-change-image-in-selected-custom-cell –

5
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
let cell = tableView.cellForRowAtIndexPath(indexPath) as! CustomTableViewCell 
cell.bgImage.image = UIImage(named: "second_image")} 
+0

謝謝你的回答! –

2

雖然爲時已晚。但我喜歡回答參與這個世界。 我在更改選定和取消選擇的表格視圖的單元格圖像時遇到問題。我這樣解決了它。 當的tableview委託方法調用然後通過

let cell = self.tableView.cellForRow(at: indexPath) as! CustomeCell 

,而不是

let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomeCell 

創建細胞的實例,並使用所需的圖像傳送到細胞ImageView的。

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    let cell = self.tableView.cellForRow(at: indexPath) as! CustomeCell 
    if indexPath.row == 0 
     cell.btnImageView.image = UIImage.init(named: "screenr.png") 
} 

override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) { 
    let cell = self.tableView.cellForRow(at: indexPath) as! CustomeCell 
    if indexPath.row == 0 
     cell.btnImageView.image = UIImage.init(named: "screen.png") 
} 
0

難道你不能簡單地使用imageView的.hightlightedimage值嗎?我做了以下在我cellForRowAt FUNC:

cell.imageView?.image = UIImage(named: "\(indexPath.row).png") 
cell.imageView?.highlightedImage = UIImage(named: "\(indexPath.row)g.png") 

而且我剛剛任命了必要的圖像「0.png」和「0g.png」等,以確保圖像和突出顯示的圖像匹配起來。當然,這是一個很小的靜態數組,如果你使用coreData和更大的數組,你可以簡單地在數據集中保存imageName和hightlightedImageName,並從那裏獲取信息。

相關問題