代码之家  ›  专栏  ›  技术社区  ›  Cue

如何更改我在tableView中设置的图像(u3;:willDisplayHeaderView:forSection:)?

  •  1
  • Cue  · 技术社区  · 6 年前

    我在tableView中为每个tableView的头部分设置了一个图像(u2;:willDisplayHeaderView:forSection:):

        let image = UIImage(named: "arrow-down")
        let imageView = UIImageView(image: image!)
        imageView.frame = CGRect(x: 220, y: 12, width: 10, height: 5)
        view.addSubview(imageView)
        header.addSubview(imageView)
    

        let frame = CGRect(x: 220, y: 12, width: 10, height: 5)
        let headerImageView = UIImageView(frame: frame)
        let image: UIImage = UIImage(named: "arrow-up")!
        headerImageView.image = image // sets the top header only
    

    如何更改特定部分显示的图像?

    我读了这个问题 How to change button image added to TableView section header 但是我没有一个按钮。

    我还发现了其他类似的问题,但我不太了解Objective-C,不能在当前Swift中轻松地转换代码而不出错。

    1 回复  |  直到 6 年前
        1
  •  4
  •   Daniel Muñoz    6 年前
    If you know the specific position, you can do it at the moment when the table is loading the elements if what you want is to add an initial image (Option 1), if what you want is that when you touch the cell change the image you can do it the following way (Option 2)
    
    -----------
    Option 1
    -----------
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
         if(indexPath.row == //index of your cell){ 
          let frame = CGRect(x: 220, y: 12, width: 10, height: 5)
          let headerImageView = UIImageView(frame: frame)
          let image: UIImage = UIImage(named: "arrow-up")!
          headerImageView.image = image
         }
    }
    
    -----------
    Option 2
    -----------
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
         if(indexPath.row == //index of your cell){ 
          let frame = CGRect(x: 220, y: 12, width: 10, height: 5)
          let headerImageView = UIImageView(frame: frame)
          let image: UIImage = UIImage(named: "arrow-up")!
          headerImageView.image = image
         }
    }