2016-01-05 51 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,并从那里获取信息。

相关问题