ios 在tableview单元格中选择更改图像 - Swift

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34621076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 08:31:41  来源:igfitidea点击:

Change image on select within tableview cell - Swift

iosswiftuitableview

提问by Katie H

I am trying to change an image within a cell when it is selected. Here is what I have in my view controller:

我试图在选择单元格时更改单元格中的图像。这是我的视图控制器中的内容:

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

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

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

}

This is where I set the 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")

}

When I select the cell, the image does not update. How do I get this to work?

当我选择单元格时,图像不会更新。我如何让这个工作?

回答by Leejay Schmidt

The issue is probably that you are calling dequeueReusableCellWithIdentifier. Try using cellForRowAtIndexPathinstead (documentation about it here). This will give you a reference to the current cell, rather than a reference to a new reusable cell.

问题可能是您正在调用dequeueReusableCellWithIdentifier. 尝试cellForRowAtIndexPath改用(有关它的文档here)。这将为您提供对当前单元格的引用,而不是对新的可重用单元格的引用。

So, according to your code above:

所以,根据你上面的代码:

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

This typecast is criticalbecause cellForRowAtIndexPathreturns UITableViewCell, which will not have the bgImagemember.

这个类型转换很关键,因为cellForRowAtIndexPath返回UITableViewCell,它不会有bgImage成员。

Also, make sure that you have a member called bgImagethat is a UIImageViewin your CustomTableViewCellclass by adding @IBOutlet strong var bgImage: UIImageView!, and that said IBOutletis connected in your storyboard/xib.

此外,请确保您有一个名为成员bgImage是一个UIImageView在你的CustomTableViewCell类加入@IBOutlet strong var bgImage: UIImageView!,并且所述IBOutlet连接在你的情节提要/厦门国际银行。

回答by Paul Razvan Berg

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

For Swift 3+:

对于 Swift 3+:

func tableView(tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! CustomTableViewCell
cell.bgImage.image = UIImage(named: "second_image")}

回答by Mr. Tann

Though it is too late. But i like to answer to participate in this world. I was having problem with changing selected and deselected cell image of a table view. I solved it this way. When tableview delegate method call then create instance of that cell by

虽然为时已晚。但我喜欢回答参与这个世界。我在更改表格视图的选定和取消选定的单元格图像时遇到问题。我是这样解决的。当 tableview 委托方法调用然后创建该单元格的实例时

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

instead of

代替

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

and use your desired image into your cell 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")
}

回答by Rashwan L

Change your didSelectRowAtIndexPathto:

将您更改didSelectRowAtIndexPath为:

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

As per default this works to change the image

默认情况下,这可以更改图像

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

回答by Zachary Fisher

Can't you simply use the .hightlightedimage value for the imageView? I'm doing the following in my cellForRowAt func:

你不能简单地为 imageView 使用 .hightlightedimage 值吗?我在 cellForRowAt 函数中执行以下操作:

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

And I've just named the necessary images "0.png" and "0g.png" etc to make sure the image and highlighted image match up. Of course, that's a small static array, if you were using coreData with a larger array you could simply hold the imageName and the hightlightedImageName in the data set and pull the info from there.

我刚刚将必要的图像命名为“0.png”和“0g.png”等,以确保图像和突出显示的图像匹配。当然,这是一个小的静态数组,如果您将 coreData 与更大的数组一起使用,您可以简单地在数据集中保存 imageName 和 hightlightedImageName 并从那里提取信息。