Swift 如何以编程方式更改单击时静态单元格中的背景图像? 静态细胞 每个单元格上都有一个图像。总共10张图片和10个单元格 如何更改单元格中的图像或在单击单元格时将其删除

Swift 如何以编程方式更改单击时静态单元格中的背景图像? 静态细胞 每个单元格上都有一个图像。总共10张图片和10个单元格 如何更改单元格中的图像或在单击单元格时将其删除,swift,image,highlight,tableviewcell,Swift,Image,Highlight,Tableviewcell,代码: 看看哪个单元格被按下了。。。索引 将此索引->“图像”+[index]放入图像[index]。图像或更改为其他。在tableView函数中(“didHighlight” 如何让这一切倒退?(“Didunhlight”) 使用委托方法响应单元格选择并更新实现中的图像​. 使用indepath参数确定点击了哪个单元格 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { //y

代码:

  • 看看哪个单元格被按下了。。。索引
  • 将此索引->“图像”+[index]放入图像[index]。图像或更改为其他。在tableView函数中(“didHighlight”

  • 如何让这一切倒退?(“Didunhlight”)

  • 使用委托方法响应单元格选择并更新实现中的图像​. 使用
    indepath
    参数确定点击了哪个单元格

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        //your logic here to change the background image
    }
    

    也可以考虑创建一个图像视图数组,以便使用索引路径轻松地进行寻址和引用。

    let imageViews = [image1, image2, image3, image4, image5, image6, image7, image8, image9, image10]
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let imageViewToUpdate = imageViews[indexPath.row] //may need to adjust the array index to fit your table view configuration
    
        //your logic here to change the background image
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        //your logic here to change the background image
    }
    
    let imageViews = [image1, image2, image3, image4, image5, image6, image7, image8, image9, image10]
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let imageViewToUpdate = imageViews[indexPath.row] //may need to adjust the array index to fit your table view configuration
    
        //your logic here to change the background image
    }