[英]Change image on select within tableview cell - 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")
}
當我選擇單元格時,圖像不會更新。 我如何讓它工作?
問題可能是您正在調用dequeueReusableCellWithIdentifier
。 嘗試使用cellForRowAtIndexPath
( 此處有關於它的文檔 )。 這將為您提供對當前單元格的引用,而不是對新的可重用單元格的引用。
所以,根據你上面的代碼:
let cell = self.tableView.cellForRowAtIndexPath(indexPath) as! CustomTableViewCell
此類型轉換是至關重要的,因為cellForRowAtIndexPath
返回UITableViewCell
,它不具有bgImage
成員。
此外,請確保您有一個名為成員bgImage
這是一個UIImageView
在CustomTableViewCell
類加入@IBOutlet strong var bgImage: UIImageView!
,並說IBOutlet
連接在你的storyboard / xib中。
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath) as! CustomTableViewCell
cell.bgImage.image = UIImage(named: "second_image")}
對於Swift 3+:
func tableView(tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! CustomTableViewCell
cell.bgImage.image = UIImage(named: "second_image")}
將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")
雖然為時已晚。 但我想回答參與這個世界。 我在更改表視圖的選定和取消選擇的單元格圖像時遇到問題。 我這樣解決了。 當tableview委托方法調用時,然后創建該單元格的實例
let cell = self.tableView.cellForRow(at: indexPath) as! CustomeCell
代替
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomeCell
並將您想要的圖像用於您的細胞圖像視圖。
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")
}
你不能簡單地使用.hightlightedimage值為imageView嗎? 我正在我的cellForRowAt函數中執行以下操作:
cell.imageView?.image = UIImage(named: "\(indexPath.row).png")
cell.imageView?.highlightedImage = UIImage(named: "\(indexPath.row)g.png")
我剛剛命名了必要的圖像“0.png”和“0g.png”等,以確保圖像和高亮顯示的圖像匹配。 當然,這是一個小型靜態數組,如果你使用的是帶有更大數組的coreData,你可以簡單地在數據集中保存imageName和hightlightedImageName,然后從那里獲取信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.