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

时间:2016-01-05 20:55:03

标签: ios swift uitableview

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

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")

}

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

5 个答案:

答案 0 :(得分:11)

问题可能是您正在呼叫dequeueReusableCellWithIdentifier。请尝试使用cellForRowAtIndexPath代替(documentation about it here)。这将为您提供对当前单元格的引用,而不是对新的可重用单元格的引用。

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

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

此类型转换严重,因为cellForRowAtIndexPath会返回UITableViewCell,而bgImage成员不会拥有bgImage

此外,通过添加UIImageView确保您的CustomTableViewCell课程中有一个名为@IBOutlet strong var bgImage: UIImageView!的成员IBOutlet,并说new Long(0)在storyboard / xib中连接。

答案 1 :(得分:6)

"Dress s"

对于Swift 3 +:

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

答案 2 :(得分:4)

将您的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")

答案 3 :(得分:3)

虽然为时已晚。但我想回答参与这个世界。 我在更改表视图的选定和取消选择的单元格图像时遇到问题。我这样解决了。 当tableview委托方法调用时,然后通过

创建该单元格的实例
#Specifying the url for the website
url <- 'https://www.immobilienscout24.de/Suche/S-4/Wohnung-Kauf/Berlin/Berlin/-/1,00-'

#Reading the HTML code from the website
webpage <- read_html(url)

#Using CSS selectors to scrap the rankings section
price_data_html <- html_nodes(webpage,'.result-list-entry__primary-criterion:nth-child(1)')

#Converting the ranking data to text
price_data <- html_text(price_data_html)

#Data-Preprocessing: removing non-numbers 
price_data<-gsub("\n","",price_data)


price_data<-gsub(" €                                                                                                                Kaufpreis                                    ",
                 "",price_data)

price_data<-gsub("                                                        ","",price_data)

price_data<-gsub(" €Kaufpreis                                    ","",price_data)

#Reviewing the data
head(price_data)

而不是

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

并将您想要的图像用于细胞图像视图。

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

答案 4 :(得分:0)

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

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

我刚刚命名了必要的图片&#34; 0.png&#34;和&#34; 0g.png&#34;等,以确保图像和突出显示的图像匹配。当然,那是一个小的静态数组,如果你使用的是带有更大数组的coreData,你可以简单地在数据集中保存imageName和hightlightedImageName,并从中拉出信息。