繁体   English   中英

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

[英]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这是一个UIImageViewCustomTableViewCell类加入@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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM