gpt4 book ai didi

ios - 如何更改我在 tableView 中设置的图像(_ :willDisplayHeaderView:forSection:)?

转载 作者:搜寻专家 更新时间:2023-11-01 06:26:52 24 4
gpt4 key购买 nike

我以这种方式在 tableView(_:willDisplayHeaderView:forSection:) 中为每个 tableView 的标题部分设置图像:

    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)

要尝试更改图像(我用它来显示折叠该部分的可能性的人字形)我尝试过这种方式,但它仅设置顶部标题,而不是我想替换特定部分中的图像(我有部分项目 Int):

    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 中轻松转换代码而不会出错。

最佳答案

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
}
}

关于ios - 如何更改我在 tableView 中设置的图像(_ :willDisplayHeaderView:forSection:)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53135941/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com